@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,270 @@
|
|
|
1
|
+
import {Controller, Get, Head, Inject, PathParams, PlatformTest, Post, QueryParams} from "@tsed/common";
|
|
2
|
+
import {UseCache, PlatformCache} from "@tsed/platform-cache";
|
|
3
|
+
import {Property} from "@tsed/schema";
|
|
4
|
+
import SuperTest from "supertest";
|
|
5
|
+
import {PlatformTestingSdkOpts} from "../interfaces";
|
|
6
|
+
|
|
7
|
+
let increment = 0;
|
|
8
|
+
|
|
9
|
+
export function testCache(options: PlatformTestingSdkOpts) {
|
|
10
|
+
let request: SuperTest.SuperTest<SuperTest.Test>;
|
|
11
|
+
|
|
12
|
+
class MyModel {
|
|
13
|
+
@Property()
|
|
14
|
+
name: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
class MyService {
|
|
18
|
+
@UseCache({
|
|
19
|
+
key: "key"
|
|
20
|
+
})
|
|
21
|
+
get(id: string) {
|
|
22
|
+
return {id: `one:${id}`};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@UseCache({
|
|
26
|
+
key(args: any[]) {
|
|
27
|
+
return `key:${args[0]}`;
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
get2(id: string) {
|
|
31
|
+
return {id: `one:${id}`};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
@Controller("/caches")
|
|
36
|
+
class TestCacheController {
|
|
37
|
+
@Inject()
|
|
38
|
+
myService: MyService;
|
|
39
|
+
|
|
40
|
+
@Get("/scenario-1")
|
|
41
|
+
@UseCache()
|
|
42
|
+
scenario1() {
|
|
43
|
+
increment++;
|
|
44
|
+
return "hello world " + increment;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@Get("/scenario-2")
|
|
48
|
+
@UseCache({ttl: 400})
|
|
49
|
+
scenario2() {
|
|
50
|
+
return {info: "hello child"};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
@Head("/scenario-3")
|
|
54
|
+
@UseCache()
|
|
55
|
+
scenario3() {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@Post("/scenario-4")
|
|
60
|
+
@UseCache()
|
|
61
|
+
scenario4() {
|
|
62
|
+
return {info: "hello child"};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
@Get("/scenario-5/:id")
|
|
66
|
+
@UseCache({ttl: 400})
|
|
67
|
+
scenario5(@PathParams("id") id: string, @QueryParams() model: MyModel) {
|
|
68
|
+
return {info: "hello child", id, ...model};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
@Get("/scenario-6/:id")
|
|
72
|
+
scenario6(@PathParams("id") id: string) {
|
|
73
|
+
return this.myService.get(id);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
@Get("/scenario-7/:id")
|
|
77
|
+
scenario7(@PathParams("id") id: string) {
|
|
78
|
+
return this.myService.get2(id);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
describe("withCache", () => {
|
|
83
|
+
beforeEach(
|
|
84
|
+
PlatformTest.bootstrap(options.server, {
|
|
85
|
+
...options,
|
|
86
|
+
logger: {},
|
|
87
|
+
mount: {
|
|
88
|
+
"/rest": [TestCacheController]
|
|
89
|
+
},
|
|
90
|
+
cache: {
|
|
91
|
+
ttl: 300,
|
|
92
|
+
store: "memory"
|
|
93
|
+
}
|
|
94
|
+
})
|
|
95
|
+
);
|
|
96
|
+
beforeEach(() => {
|
|
97
|
+
request = SuperTest(PlatformTest.callback());
|
|
98
|
+
});
|
|
99
|
+
afterEach(PlatformTest.reset);
|
|
100
|
+
|
|
101
|
+
describe("scenario 1: GET /rest/caches/scenario-1", () => {
|
|
102
|
+
it("should return data with cached response", async () => {
|
|
103
|
+
const response = await request.get("/rest/caches/scenario-1").expect(200);
|
|
104
|
+
const response2 = await request.get("/rest/caches/scenario-1").expect(200);
|
|
105
|
+
|
|
106
|
+
expect(response.text).toContain("hello world");
|
|
107
|
+
expect(response.headers["cache-control"]).toMatch(/max-age=300/);
|
|
108
|
+
expect(response.headers["x-cached"]).toBeUndefined();
|
|
109
|
+
|
|
110
|
+
expect(response2.text).toContain("hello world");
|
|
111
|
+
expect(response2.headers["cache-control"]).toMatch(/max-age=300/);
|
|
112
|
+
expect(response2.headers["x-cached"]).toEqual("true");
|
|
113
|
+
expect(response2.headers["etag"]).toEqual(response.headers["etag"]);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("should return 304 when content isn't modified", async () => {
|
|
117
|
+
const platformCache = PlatformTest.get<PlatformCache>(PlatformCache);
|
|
118
|
+
|
|
119
|
+
const response = await request.get("/rest/caches/scenario-1").expect(200);
|
|
120
|
+
|
|
121
|
+
await request.get("/rest/caches/scenario-1").set("if-none-match", response.headers["etag"]).expect(304);
|
|
122
|
+
|
|
123
|
+
await platformCache.reset();
|
|
124
|
+
|
|
125
|
+
await request.get("/rest/caches/scenario-1").set("if-none-match", response.headers["etag"]).expect(200);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("should return fresh data if cache-control is set to no-cache", async () => {
|
|
129
|
+
await request.get("/rest/caches/scenario-1").expect(200);
|
|
130
|
+
const response2 = await request.get("/rest/caches/scenario-1").set("cache-control", "no-cache").expect(200);
|
|
131
|
+
|
|
132
|
+
expect(response2.text).toContain("hello world");
|
|
133
|
+
expect(response2.headers["cache-control"]).toMatch(/max-age=300/);
|
|
134
|
+
expect(response2.headers["x-cached"]).toBeUndefined();
|
|
135
|
+
expect(response2.headers["etag"]).toEqual(response2.headers["etag"]);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
describe("scenario 2: GET /rest/caches/scenario-2", () => {
|
|
139
|
+
it("should return data with cached response", async () => {
|
|
140
|
+
const response = await request.get("/rest/caches/scenario-2").expect(200);
|
|
141
|
+
const response2 = await request.get("/rest/caches/scenario-2").expect(200);
|
|
142
|
+
|
|
143
|
+
expect(response.body).toEqual({info: "hello child"});
|
|
144
|
+
expect(response.headers["cache-control"]).toMatch(/max-age=400/);
|
|
145
|
+
expect(response.headers["x-cached"]).toBeUndefined();
|
|
146
|
+
|
|
147
|
+
expect(response2.body).toEqual({info: "hello child"});
|
|
148
|
+
expect(response2.headers["cache-control"]).toMatch(/max-age=400/);
|
|
149
|
+
expect(response2.headers["x-cached"]).toEqual("true");
|
|
150
|
+
expect(response2.headers["etag"]).toEqual(response.headers["etag"]);
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
describe("scenario 4: POST /rest/caches/scenario-4", () => {
|
|
154
|
+
it("should not cache POST method", async () => {
|
|
155
|
+
const response = await request.post("/rest/caches/scenario-4").expect(200);
|
|
156
|
+
const response2 = await request.post("/rest/caches/scenario-4").expect(200);
|
|
157
|
+
|
|
158
|
+
expect(response.body).toEqual({
|
|
159
|
+
info: "hello child"
|
|
160
|
+
});
|
|
161
|
+
expect(response.headers["cache-control"]).toBeUndefined();
|
|
162
|
+
expect(response.headers["x-cached"]).toBeUndefined();
|
|
163
|
+
|
|
164
|
+
expect(response2.body).toEqual({
|
|
165
|
+
info: "hello child"
|
|
166
|
+
});
|
|
167
|
+
expect(response.headers["cache-control"]).toBeUndefined();
|
|
168
|
+
expect(response2.headers["x-cached"]).toBeUndefined();
|
|
169
|
+
expect(response2.headers["etag"]).toEqual(response.headers["etag"]);
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
describe("scenario 5: GET /rest/caches/scenario-5", () => {
|
|
173
|
+
it("should return data with then cache response", async () => {
|
|
174
|
+
const response = await request.get("/rest/caches/scenario-5/1?name=1").expect(200);
|
|
175
|
+
const response2 = await request.get("/rest/caches/scenario-5/1?name=1").expect(200);
|
|
176
|
+
const response3 = await request.get("/rest/caches/scenario-5/2?name=2").expect(200);
|
|
177
|
+
const response4 = await request.get("/rest/caches/scenario-5/2?name=2").expect(200);
|
|
178
|
+
const response5 = await request.get("/rest/caches/scenario-5/2?name=3").expect(200);
|
|
179
|
+
|
|
180
|
+
expect(response.body).toEqual({
|
|
181
|
+
id: "1",
|
|
182
|
+
info: "hello child",
|
|
183
|
+
name: "1"
|
|
184
|
+
});
|
|
185
|
+
expect(response2.body).toEqual({
|
|
186
|
+
id: "1",
|
|
187
|
+
info: "hello child",
|
|
188
|
+
name: "1"
|
|
189
|
+
});
|
|
190
|
+
expect(response.headers["x-cached"]).toBeUndefined();
|
|
191
|
+
expect(response2.headers["x-cached"]).toEqual("true");
|
|
192
|
+
expect(response.body).toEqual(response2.body);
|
|
193
|
+
|
|
194
|
+
expect(response3.body).toEqual({
|
|
195
|
+
id: "2",
|
|
196
|
+
info: "hello child",
|
|
197
|
+
name: "2"
|
|
198
|
+
});
|
|
199
|
+
expect(response4.body).toEqual({
|
|
200
|
+
id: "2",
|
|
201
|
+
info: "hello child",
|
|
202
|
+
name: "2"
|
|
203
|
+
});
|
|
204
|
+
expect(response3.headers["x-cached"]).toBeUndefined();
|
|
205
|
+
expect(response4.headers["x-cached"]).toEqual("true");
|
|
206
|
+
expect(response3.body).toEqual(response4.body);
|
|
207
|
+
|
|
208
|
+
expect(response2.body).not.toEqual(response4);
|
|
209
|
+
expect(response5.body).not.toEqual(response4);
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
describe("scenario 6: GET /rest/caches/scenario-6", () => {
|
|
213
|
+
it("should return data from cached service", async () => {
|
|
214
|
+
const response = await request.get("/rest/caches/scenario-6/1").expect(200);
|
|
215
|
+
const response2 = await request.get("/rest/caches/scenario-6/2").expect(200);
|
|
216
|
+
|
|
217
|
+
expect(response.body).toEqual({id: "one:1"});
|
|
218
|
+
expect(response2.body).toEqual({id: "one:1"});
|
|
219
|
+
expect(response.headers["x-cached"]).toBeUndefined();
|
|
220
|
+
expect(response2.headers["x-cached"]).toBeUndefined();
|
|
221
|
+
expect(response.body).toEqual(response2.body);
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
describe("scenario 7: GET /rest/caches/scenario-7", () => {
|
|
225
|
+
it("should return data from cached service", async () => {
|
|
226
|
+
const response = await request.get("/rest/caches/scenario-7/1").expect(200);
|
|
227
|
+
const response2 = await request.get("/rest/caches/scenario-7/2").expect(200);
|
|
228
|
+
const response3 = await request.get("/rest/caches/scenario-7/2").expect(200);
|
|
229
|
+
|
|
230
|
+
expect(response.body).toEqual({id: "one:1"});
|
|
231
|
+
expect(response2.body).toEqual({
|
|
232
|
+
id: "one:2"
|
|
233
|
+
});
|
|
234
|
+
expect(response3.body).toEqual({
|
|
235
|
+
id: "one:2"
|
|
236
|
+
});
|
|
237
|
+
expect(response.body).not.toEqual(response2.body);
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
});
|
|
241
|
+
describe("withoutCache", () => {
|
|
242
|
+
beforeEach(
|
|
243
|
+
PlatformTest.bootstrap(options.server, {
|
|
244
|
+
...options,
|
|
245
|
+
logger: {},
|
|
246
|
+
cache: false,
|
|
247
|
+
mount: {
|
|
248
|
+
"/rest": [TestCacheController]
|
|
249
|
+
}
|
|
250
|
+
})
|
|
251
|
+
);
|
|
252
|
+
beforeEach(() => {
|
|
253
|
+
request = SuperTest(PlatformTest.callback());
|
|
254
|
+
});
|
|
255
|
+
afterEach(PlatformTest.reset);
|
|
256
|
+
|
|
257
|
+
describe("scenario 1: GET /rest/caches/scenario-1", () => {
|
|
258
|
+
it("should return data with cached response", async () => {
|
|
259
|
+
const response = await request.get("/rest/caches/scenario-1").expect(200);
|
|
260
|
+
const response2 = await request.get("/rest/caches/scenario-1").expect(200);
|
|
261
|
+
|
|
262
|
+
expect(response.text).toContain("hello world");
|
|
263
|
+
expect(response.headers["x-cached"]).toBeUndefined();
|
|
264
|
+
|
|
265
|
+
expect(response2.text).toContain("hello world");
|
|
266
|
+
expect(response2.headers["x-cached"]).toBeUndefined();
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {Controller, Get, PlatformTest} from "@tsed/common";
|
|
2
|
+
import SuperTest from "supertest";
|
|
3
|
+
import {PlatformTestingSdkOpts} from "../interfaces";
|
|
4
|
+
|
|
5
|
+
export function testChildrenControllers(options: PlatformTestingSdkOpts) {
|
|
6
|
+
let request: SuperTest.SuperTest<SuperTest.Test>;
|
|
7
|
+
|
|
8
|
+
@Controller("/children")
|
|
9
|
+
class TestChildController {
|
|
10
|
+
@Get("/scenario-2")
|
|
11
|
+
get() {
|
|
12
|
+
return "hello child";
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@Controller({
|
|
17
|
+
path: "/controllers",
|
|
18
|
+
children: [TestChildController]
|
|
19
|
+
})
|
|
20
|
+
class TestController {
|
|
21
|
+
@Get("/scenario-1")
|
|
22
|
+
scenario1() {
|
|
23
|
+
return "hello world";
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
beforeEach(
|
|
28
|
+
PlatformTest.bootstrap(options.server, {
|
|
29
|
+
...options,
|
|
30
|
+
mount: {
|
|
31
|
+
"/rest": [TestController]
|
|
32
|
+
}
|
|
33
|
+
})
|
|
34
|
+
);
|
|
35
|
+
beforeEach(() => {
|
|
36
|
+
request = SuperTest(PlatformTest.callback());
|
|
37
|
+
});
|
|
38
|
+
afterEach(PlatformTest.reset);
|
|
39
|
+
|
|
40
|
+
describe("scenario 1: GET /rest/controllers/scenario-1", () => {
|
|
41
|
+
it("should return a response from method", async () => {
|
|
42
|
+
const response = await request.get("/rest/controllers/scenario-1").expect(200);
|
|
43
|
+
|
|
44
|
+
expect(response.text).toEqual("hello world");
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe("scenario 2: GET /rest/controllers/children/scenario-2", () => {
|
|
49
|
+
it("should return a response from method", async () => {
|
|
50
|
+
const response = await request.get("/rest/controllers/children/scenario-2").expect(200);
|
|
51
|
+
|
|
52
|
+
expect(response.text).toEqual("hello child");
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {Controller, CookiesParams, Get, PlatformTest} from "@tsed/common";
|
|
2
|
+
import SuperTest from "supertest";
|
|
3
|
+
import {PlatformTestingSdkOpts} from "../interfaces";
|
|
4
|
+
|
|
5
|
+
@Controller("/cookies")
|
|
6
|
+
export class CookiesCtrl {
|
|
7
|
+
/**
|
|
8
|
+
* Handle request and get auth from cookies
|
|
9
|
+
* @param authorization
|
|
10
|
+
*/
|
|
11
|
+
@Get("/scenario-1")
|
|
12
|
+
public scenario1(@CookiesParams("authorization") authorization: string) {
|
|
13
|
+
return {authorization};
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function testCookies(options: PlatformTestingSdkOpts) {
|
|
18
|
+
let request: SuperTest.SuperTest<SuperTest.Test>;
|
|
19
|
+
beforeAll(
|
|
20
|
+
PlatformTest.bootstrap(options.server, {
|
|
21
|
+
...options,
|
|
22
|
+
mount: {
|
|
23
|
+
"/rest": [CookiesCtrl]
|
|
24
|
+
}
|
|
25
|
+
})
|
|
26
|
+
);
|
|
27
|
+
beforeAll(() => {
|
|
28
|
+
request = SuperTest(PlatformTest.callback());
|
|
29
|
+
});
|
|
30
|
+
afterAll(PlatformTest.reset);
|
|
31
|
+
it("Scenario 1: GET /rest/cookies/scenario-1", async () => {
|
|
32
|
+
const {body} = await request.get("/rest/cookies/scenario-1").set("Cookie", "authorization=eOIjdkk").expect(200);
|
|
33
|
+
|
|
34
|
+
expect(body.authorization).toEqual("eOIjdkk");
|
|
35
|
+
});
|
|
36
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import {Controller, Get, PathParams, PlatformTest, QueryParams} from "@tsed/common";
|
|
2
|
+
import {getSpec, SpecTypes} from "@tsed/schema";
|
|
3
|
+
import SuperTest from "supertest";
|
|
4
|
+
import {PlatformTestingSdkOpts} from "../interfaces";
|
|
5
|
+
|
|
6
|
+
export abstract class TestBaseController {
|
|
7
|
+
@Get("/")
|
|
8
|
+
scenario1(@QueryParams("search") search: string) {
|
|
9
|
+
return {
|
|
10
|
+
search
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
@Get("/override")
|
|
15
|
+
scenario3(@QueryParams("q") q: string) {
|
|
16
|
+
return {data: q};
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
@Controller("/test")
|
|
21
|
+
export class TestChildController extends TestBaseController {
|
|
22
|
+
@Get("/:id")
|
|
23
|
+
scenario2(@PathParams("id") id: string) {
|
|
24
|
+
return {id};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
@Get("/override")
|
|
28
|
+
scenario3(@QueryParams("s") s: string) {
|
|
29
|
+
return {data: s};
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function testCtrlInheritance(options: PlatformTestingSdkOpts) {
|
|
34
|
+
let request: SuperTest.SuperTest<SuperTest.Test>;
|
|
35
|
+
|
|
36
|
+
beforeAll(
|
|
37
|
+
PlatformTest.bootstrap(options.server, {
|
|
38
|
+
...options,
|
|
39
|
+
mount: {
|
|
40
|
+
"/rest": [TestChildController]
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
);
|
|
44
|
+
beforeAll(() => {
|
|
45
|
+
request = SuperTest(PlatformTest.callback());
|
|
46
|
+
});
|
|
47
|
+
afterAll(PlatformTest.reset);
|
|
48
|
+
|
|
49
|
+
it("Scenario1: should call inherited method", async () => {
|
|
50
|
+
const {body} = await request.get("/rest/test/?search=test").expect(200);
|
|
51
|
+
expect(body).toEqual({
|
|
52
|
+
search: "test"
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("Scenario2: should the Child method", async () => {
|
|
57
|
+
const {body} = await request.get("/rest/test/1").expect(200);
|
|
58
|
+
expect(body).toEqual({id: "1"});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("Scenario2: should call the Child method and not the base method", async () => {
|
|
62
|
+
const {body} = await request.get("/rest/test/1").expect(200);
|
|
63
|
+
expect(body).toEqual({id: "1"});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("should generate swagger json", () => {
|
|
67
|
+
expect(getSpec(TestChildController, {specType: SpecTypes.OPENAPI})).toEqual({
|
|
68
|
+
paths: {
|
|
69
|
+
"/test": {
|
|
70
|
+
get: {
|
|
71
|
+
operationId: "testBaseControllerScenario1",
|
|
72
|
+
parameters: [
|
|
73
|
+
{
|
|
74
|
+
in: "query",
|
|
75
|
+
name: "search",
|
|
76
|
+
required: false,
|
|
77
|
+
schema: {
|
|
78
|
+
type: "string"
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
],
|
|
82
|
+
responses: {
|
|
83
|
+
"200": {
|
|
84
|
+
description: "Success"
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
tags: ["TestChildController"]
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
"/test/override": {
|
|
91
|
+
get: {
|
|
92
|
+
operationId: "testChildControllerScenario3",
|
|
93
|
+
parameters: [
|
|
94
|
+
{
|
|
95
|
+
in: "query",
|
|
96
|
+
name: "s",
|
|
97
|
+
required: false,
|
|
98
|
+
schema: {
|
|
99
|
+
type: "string"
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
],
|
|
103
|
+
responses: {
|
|
104
|
+
"200": {
|
|
105
|
+
description: "Success"
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
tags: ["TestChildController"]
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
"/test/{id}": {
|
|
112
|
+
get: {
|
|
113
|
+
operationId: "testChildControllerScenario2",
|
|
114
|
+
parameters: [
|
|
115
|
+
{
|
|
116
|
+
in: "path",
|
|
117
|
+
name: "id",
|
|
118
|
+
required: true,
|
|
119
|
+
schema: {
|
|
120
|
+
type: "string"
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
],
|
|
124
|
+
responses: {
|
|
125
|
+
"200": {
|
|
126
|
+
description: "Success"
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
tags: ["TestChildController"]
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
tags: [
|
|
134
|
+
{
|
|
135
|
+
name: "TestChildController"
|
|
136
|
+
}
|
|
137
|
+
]
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import {PlatformTest} from "@tsed/common";
|
|
2
|
+
import SuperTest from "supertest";
|
|
3
|
+
import {PlatformTestingSdkOpts} from "../interfaces";
|
|
4
|
+
|
|
5
|
+
export function testCustom404(options: PlatformTestingSdkOpts) {
|
|
6
|
+
class CustomServer extends options.server {}
|
|
7
|
+
|
|
8
|
+
let request: SuperTest.SuperTest<SuperTest.Test>;
|
|
9
|
+
beforeAll(
|
|
10
|
+
PlatformTest.bootstrap(CustomServer, {
|
|
11
|
+
...options,
|
|
12
|
+
mount: {
|
|
13
|
+
"/rest": []
|
|
14
|
+
}
|
|
15
|
+
})
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
beforeAll(() => {
|
|
19
|
+
request = SuperTest(PlatformTest.callback());
|
|
20
|
+
});
|
|
21
|
+
afterAll(PlatformTest.reset);
|
|
22
|
+
|
|
23
|
+
it("Scenario 1: GET /", async () => {
|
|
24
|
+
const response: any = await request.get("/").expect(404);
|
|
25
|
+
|
|
26
|
+
expect(response.body).toEqual({
|
|
27
|
+
name: "NOT_FOUND",
|
|
28
|
+
message: 'Resource "/" not found',
|
|
29
|
+
status: 404,
|
|
30
|
+
errors: []
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
}
|