@tsed/platform-test-sdk 7.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (231) hide show
  1. package/lib/cjs/components/PlatformTestSdk.js +31 -0
  2. package/lib/cjs/components/PlatformTestSdk.js.map +1 -0
  3. package/lib/cjs/data/swagger.json +32 -0
  4. package/lib/cjs/errors/CustomBadRequest.js +18 -0
  5. package/lib/cjs/errors/CustomBadRequest.js.map +1 -0
  6. package/lib/cjs/errors/CustomInternalError.js +18 -0
  7. package/lib/cjs/errors/CustomInternalError.js.map +1 -0
  8. package/lib/cjs/index.js +42 -0
  9. package/lib/cjs/index.js.map +1 -0
  10. package/lib/cjs/interfaces/PlatformTestingSdkOpts.js +3 -0
  11. package/lib/cjs/interfaces/PlatformTestingSdkOpts.js.map +1 -0
  12. package/lib/cjs/interfaces/index.js +8 -0
  13. package/lib/cjs/interfaces/index.js.map +1 -0
  14. package/lib/cjs/modules/feature/FeatureModule.js +17 -0
  15. package/lib/cjs/modules/feature/FeatureModule.js.map +1 -0
  16. package/lib/cjs/modules/feature/controllers/FeatureController.js +23 -0
  17. package/lib/cjs/modules/feature/controllers/FeatureController.js.map +1 -0
  18. package/lib/cjs/package.json +3 -0
  19. package/lib/cjs/tests/exports.js +66 -0
  20. package/lib/cjs/tests/exports.js.map +1 -0
  21. package/lib/cjs/tests/testAcceptMime.js +103 -0
  22. package/lib/cjs/tests/testAcceptMime.js.map +1 -0
  23. package/lib/cjs/tests/testAuth.js +343 -0
  24. package/lib/cjs/tests/testAuth.js.map +1 -0
  25. package/lib/cjs/tests/testBodyParams.js +526 -0
  26. package/lib/cjs/tests/testBodyParams.js.map +1 -0
  27. package/lib/cjs/tests/testCache.js +292 -0
  28. package/lib/cjs/tests/testCache.js.map +1 -0
  29. package/lib/cjs/tests/testChildrenControllers.js +64 -0
  30. package/lib/cjs/tests/testChildrenControllers.js.map +1 -0
  31. package/lib/cjs/tests/testCookies.js +45 -0
  32. package/lib/cjs/tests/testCookies.js.map +1 -0
  33. package/lib/cjs/tests/testCtrlInheritance.js +161 -0
  34. package/lib/cjs/tests/testCtrlInheritance.js.map +1 -0
  35. package/lib/cjs/tests/testCustom404.js +32 -0
  36. package/lib/cjs/tests/testCustom404.js.map +1 -0
  37. package/lib/cjs/tests/testErrors.js +252 -0
  38. package/lib/cjs/tests/testErrors.js.map +1 -0
  39. package/lib/cjs/tests/testHandlers.js +110 -0
  40. package/lib/cjs/tests/testHandlers.js.map +1 -0
  41. package/lib/cjs/tests/testHeaderParams.js +166 -0
  42. package/lib/cjs/tests/testHeaderParams.js.map +1 -0
  43. package/lib/cjs/tests/testHeaders.js +101 -0
  44. package/lib/cjs/tests/testHeaders.js.map +1 -0
  45. package/lib/cjs/tests/testInheritanceController.js +173 -0
  46. package/lib/cjs/tests/testInheritanceController.js.map +1 -0
  47. package/lib/cjs/tests/testLocals.js +63 -0
  48. package/lib/cjs/tests/testLocals.js.map +1 -0
  49. package/lib/cjs/tests/testLocation.js +42 -0
  50. package/lib/cjs/tests/testLocation.js.map +1 -0
  51. package/lib/cjs/tests/testMiddlewares.js +210 -0
  52. package/lib/cjs/tests/testMiddlewares.js.map +1 -0
  53. package/lib/cjs/tests/testModule.js +49 -0
  54. package/lib/cjs/tests/testModule.js.map +1 -0
  55. package/lib/cjs/tests/testMulter.js +162 -0
  56. package/lib/cjs/tests/testMulter.js.map +1 -0
  57. package/lib/cjs/tests/testPathParams.js +142 -0
  58. package/lib/cjs/tests/testPathParams.js.map +1 -0
  59. package/lib/cjs/tests/testQueryParams.js +354 -0
  60. package/lib/cjs/tests/testQueryParams.js.map +1 -0
  61. package/lib/cjs/tests/testRedirect.js +143 -0
  62. package/lib/cjs/tests/testRedirect.js.map +1 -0
  63. package/lib/cjs/tests/testResponse.js +622 -0
  64. package/lib/cjs/tests/testResponse.js.map +1 -0
  65. package/lib/cjs/tests/testResponseFilter.js +107 -0
  66. package/lib/cjs/tests/testResponseFilter.js.map +1 -0
  67. package/lib/cjs/tests/testRouting.js +75 -0
  68. package/lib/cjs/tests/testRouting.js.map +1 -0
  69. package/lib/cjs/tests/testScopeRequest.js +116 -0
  70. package/lib/cjs/tests/testScopeRequest.js.map +1 -0
  71. package/lib/cjs/tests/testSession.js +128 -0
  72. package/lib/cjs/tests/testSession.js.map +1 -0
  73. package/lib/cjs/tests/testStatics.js +58 -0
  74. package/lib/cjs/tests/testStatics.js.map +1 -0
  75. package/lib/cjs/tests/testView.js +83 -0
  76. package/lib/cjs/tests/testView.js.map +1 -0
  77. package/lib/esm/components/PlatformTestSdk.js +27 -0
  78. package/lib/esm/components/PlatformTestSdk.js.map +1 -0
  79. package/lib/esm/data/swagger.json +32 -0
  80. package/lib/esm/errors/CustomBadRequest.js +14 -0
  81. package/lib/esm/errors/CustomBadRequest.js.map +1 -0
  82. package/lib/esm/errors/CustomInternalError.js +14 -0
  83. package/lib/esm/errors/CustomInternalError.js.map +1 -0
  84. package/lib/esm/index.js +39 -0
  85. package/lib/esm/index.js.map +1 -0
  86. package/lib/esm/interfaces/PlatformTestingSdkOpts.js +2 -0
  87. package/lib/esm/interfaces/PlatformTestingSdkOpts.js.map +1 -0
  88. package/lib/esm/interfaces/index.js +5 -0
  89. package/lib/esm/interfaces/index.js.map +1 -0
  90. package/lib/esm/modules/feature/FeatureModule.js +14 -0
  91. package/lib/esm/modules/feature/FeatureModule.js.map +1 -0
  92. package/lib/esm/modules/feature/controllers/FeatureController.js +20 -0
  93. package/lib/esm/modules/feature/controllers/FeatureController.js.map +1 -0
  94. package/lib/esm/package.json +3 -0
  95. package/lib/esm/tests/exports.js +63 -0
  96. package/lib/esm/tests/exports.js.map +1 -0
  97. package/lib/esm/tests/testAcceptMime.js +99 -0
  98. package/lib/esm/tests/testAcceptMime.js.map +1 -0
  99. package/lib/esm/tests/testAuth.js +338 -0
  100. package/lib/esm/tests/testAuth.js.map +1 -0
  101. package/lib/esm/tests/testBodyParams.js +522 -0
  102. package/lib/esm/tests/testBodyParams.js.map +1 -0
  103. package/lib/esm/tests/testCache.js +288 -0
  104. package/lib/esm/tests/testCache.js.map +1 -0
  105. package/lib/esm/tests/testChildrenControllers.js +60 -0
  106. package/lib/esm/tests/testChildrenControllers.js.map +1 -0
  107. package/lib/esm/tests/testCookies.js +41 -0
  108. package/lib/esm/tests/testCookies.js.map +1 -0
  109. package/lib/esm/tests/testCtrlInheritance.js +156 -0
  110. package/lib/esm/tests/testCtrlInheritance.js.map +1 -0
  111. package/lib/esm/tests/testCustom404.js +27 -0
  112. package/lib/esm/tests/testCustom404.js.map +1 -0
  113. package/lib/esm/tests/testErrors.js +248 -0
  114. package/lib/esm/tests/testErrors.js.map +1 -0
  115. package/lib/esm/tests/testHandlers.js +105 -0
  116. package/lib/esm/tests/testHandlers.js.map +1 -0
  117. package/lib/esm/tests/testHeaderParams.js +162 -0
  118. package/lib/esm/tests/testHeaderParams.js.map +1 -0
  119. package/lib/esm/tests/testHeaders.js +97 -0
  120. package/lib/esm/tests/testHeaders.js.map +1 -0
  121. package/lib/esm/tests/testInheritanceController.js +167 -0
  122. package/lib/esm/tests/testInheritanceController.js.map +1 -0
  123. package/lib/esm/tests/testLocals.js +59 -0
  124. package/lib/esm/tests/testLocals.js.map +1 -0
  125. package/lib/esm/tests/testLocation.js +38 -0
  126. package/lib/esm/tests/testLocation.js.map +1 -0
  127. package/lib/esm/tests/testMiddlewares.js +206 -0
  128. package/lib/esm/tests/testMiddlewares.js.map +1 -0
  129. package/lib/esm/tests/testModule.js +45 -0
  130. package/lib/esm/tests/testModule.js.map +1 -0
  131. package/lib/esm/tests/testMulter.js +156 -0
  132. package/lib/esm/tests/testMulter.js.map +1 -0
  133. package/lib/esm/tests/testPathParams.js +138 -0
  134. package/lib/esm/tests/testPathParams.js.map +1 -0
  135. package/lib/esm/tests/testQueryParams.js +349 -0
  136. package/lib/esm/tests/testQueryParams.js.map +1 -0
  137. package/lib/esm/tests/testRedirect.js +139 -0
  138. package/lib/esm/tests/testRedirect.js.map +1 -0
  139. package/lib/esm/tests/testResponse.js +615 -0
  140. package/lib/esm/tests/testResponse.js.map +1 -0
  141. package/lib/esm/tests/testResponseFilter.js +103 -0
  142. package/lib/esm/tests/testResponseFilter.js.map +1 -0
  143. package/lib/esm/tests/testRouting.js +71 -0
  144. package/lib/esm/tests/testRouting.js.map +1 -0
  145. package/lib/esm/tests/testScopeRequest.js +112 -0
  146. package/lib/esm/tests/testScopeRequest.js.map +1 -0
  147. package/lib/esm/tests/testSession.js +122 -0
  148. package/lib/esm/tests/testSession.js.map +1 -0
  149. package/lib/esm/tests/testStatics.js +54 -0
  150. package/lib/esm/tests/testStatics.js.map +1 -0
  151. package/lib/esm/tests/testView.js +79 -0
  152. package/lib/esm/tests/testView.js.map +1 -0
  153. package/lib/types/components/PlatformTestSdk.d.ts +9 -0
  154. package/lib/types/errors/CustomBadRequest.d.ts +8 -0
  155. package/lib/types/errors/CustomInternalError.d.ts +8 -0
  156. package/lib/types/index.d.ts +38 -0
  157. package/lib/types/interfaces/PlatformTestingSdkOpts.d.ts +8 -0
  158. package/lib/types/interfaces/index.d.ts +4 -0
  159. package/lib/types/modules/feature/FeatureModule.d.ts +2 -0
  160. package/lib/types/modules/feature/controllers/FeatureController.d.ts +3 -0
  161. package/lib/types/tests/exports.d.ts +2 -0
  162. package/lib/types/tests/testAcceptMime.d.ts +2 -0
  163. package/lib/types/tests/testAuth.d.ts +8 -0
  164. package/lib/types/tests/testBodyParams.d.ts +3 -0
  165. package/lib/types/tests/testCache.d.ts +2 -0
  166. package/lib/types/tests/testChildrenControllers.d.ts +2 -0
  167. package/lib/types/tests/testCookies.d.ts +11 -0
  168. package/lib/types/tests/testCtrlInheritance.d.ts +18 -0
  169. package/lib/types/tests/testCustom404.d.ts +2 -0
  170. package/lib/types/tests/testErrors.d.ts +19 -0
  171. package/lib/types/tests/testHandlers.d.ts +12 -0
  172. package/lib/types/tests/testHeaderParams.d.ts +16 -0
  173. package/lib/types/tests/testHeaders.d.ts +8 -0
  174. package/lib/types/tests/testInheritanceController.d.ts +28 -0
  175. package/lib/types/tests/testLocals.d.ts +2 -0
  176. package/lib/types/tests/testLocation.d.ts +2 -0
  177. package/lib/types/tests/testMiddlewares.d.ts +8 -0
  178. package/lib/types/tests/testModule.d.ts +2 -0
  179. package/lib/types/tests/testMulter.d.ts +21 -0
  180. package/lib/types/tests/testPathParams.d.ts +2 -0
  181. package/lib/types/tests/testQueryParams.d.ts +6 -0
  182. package/lib/types/tests/testRedirect.d.ts +2 -0
  183. package/lib/types/tests/testResponse.d.ts +16 -0
  184. package/lib/types/tests/testResponseFilter.d.ts +2 -0
  185. package/lib/types/tests/testRouting.d.ts +7 -0
  186. package/lib/types/tests/testScopeRequest.d.ts +12 -0
  187. package/lib/types/tests/testSession.d.ts +16 -0
  188. package/lib/types/tests/testStatics.d.ts +2 -0
  189. package/lib/types/tests/testView.d.ts +2 -0
  190. package/package.json +44 -0
  191. package/src/components/PlatformTestSdk.ts +29 -0
  192. package/src/data/file.txt +1 -0
  193. package/src/data/response.data.json +3 -0
  194. package/src/data/swagger.json +32 -0
  195. package/src/errors/CustomBadRequest.ts +16 -0
  196. package/src/errors/CustomInternalError.ts +16 -0
  197. package/src/index.ts +39 -0
  198. package/src/interfaces/PlatformTestingSdkOpts.ts +10 -0
  199. package/src/interfaces/index.ts +5 -0
  200. package/src/modules/feature/FeatureModule.ts +9 -0
  201. package/src/modules/feature/controllers/FeatureController.ts +11 -0
  202. package/src/tests/exports.ts +69 -0
  203. package/src/tests/testAcceptMime.ts +95 -0
  204. package/src/tests/testAuth.ts +339 -0
  205. package/src/tests/testBodyParams.ts +455 -0
  206. package/src/tests/testCache.ts +270 -0
  207. package/src/tests/testChildrenControllers.ts +55 -0
  208. package/src/tests/testCookies.ts +36 -0
  209. package/src/tests/testCtrlInheritance.ts +140 -0
  210. package/src/tests/testCustom404.ts +33 -0
  211. package/src/tests/testErrors.ts +223 -0
  212. package/src/tests/testHandlers.ts +92 -0
  213. package/src/tests/testHeaderParams.ts +149 -0
  214. package/src/tests/testHeaders.ts +91 -0
  215. package/src/tests/testInheritanceController.ts +156 -0
  216. package/src/tests/testLocals.ts +46 -0
  217. package/src/tests/testLocation.ts +37 -0
  218. package/src/tests/testMiddlewares.ts +159 -0
  219. package/src/tests/testModule.ts +46 -0
  220. package/src/tests/testMulter.ts +140 -0
  221. package/src/tests/testPathParams.ts +122 -0
  222. package/src/tests/testQueryParams.ts +323 -0
  223. package/src/tests/testRedirect.ts +124 -0
  224. package/src/tests/testResponse.ts +558 -0
  225. package/src/tests/testResponseFilter.ts +101 -0
  226. package/src/tests/testRouting.ts +65 -0
  227. package/src/tests/testScopeRequest.ts +111 -0
  228. package/src/tests/testSession.ts +104 -0
  229. package/src/tests/testStatics.ts +61 -0
  230. package/src/tests/testView.ts +68 -0
  231. package/tsconfig.compile.esm.json +12 -0
@@ -0,0 +1,65 @@
1
+ import {Controller, Get, PlatformTest, Put} from "@tsed/common";
2
+ import SuperTest from "supertest";
3
+ import {PlatformTestingSdkOpts} from "../interfaces";
4
+
5
+ const stub = jest.fn();
6
+
7
+ @Controller("/routing")
8
+ export class TestRoutingController {
9
+ @Get("/1")
10
+ scenario1() {
11
+ stub("scenario1");
12
+
13
+ return "test1";
14
+ }
15
+
16
+ @Put("") // empty string has an effect on the calls orders
17
+ scenario2() {
18
+ stub("scenario2");
19
+
20
+ return "shouldNotRun";
21
+ }
22
+
23
+ @Get("/3")
24
+ scenario3() {
25
+ stub("scenario3");
26
+
27
+ return "test3";
28
+ }
29
+ }
30
+
31
+ export function testRouting(options: PlatformTestingSdkOpts) {
32
+ let request: SuperTest.SuperTest<SuperTest.Test>;
33
+
34
+ beforeAll(
35
+ PlatformTest.bootstrap(options.server, {
36
+ ...options,
37
+ mount: {
38
+ "/rest": [TestRoutingController]
39
+ }
40
+ })
41
+ );
42
+ beforeAll(() => {
43
+ request = SuperTest(PlatformTest.callback());
44
+ });
45
+ afterAll(PlatformTest.reset);
46
+ afterEach(() => jest.resetAllMocks());
47
+
48
+ it("Scenario1: should call scenario1 only", async () => {
49
+ const {text} = await request.get("/rest/routing/1").expect(200);
50
+ expect(text).toEqual("test1");
51
+ expect(stub).toHaveBeenCalledWith("scenario1");
52
+ });
53
+
54
+ it("Scenario2: should call scenario2 only", async () => {
55
+ const {text} = await request.put("/rest/routing/").expect(200);
56
+ expect(text).toEqual("shouldNotRun");
57
+ expect(stub).toHaveBeenCalledWith("scenario2");
58
+ });
59
+
60
+ it("Scenario3: should call scenario3 only", async () => {
61
+ const {text} = await request.get("/rest/routing/3").expect(200);
62
+ expect(text).toEqual("test3");
63
+ expect(stub).toHaveBeenCalledWith("scenario3");
64
+ });
65
+ }
@@ -0,0 +1,111 @@
1
+ import {Controller, Get, PathParams, PlatformTest, ProviderScope, Scope, Service} from "@tsed/common";
2
+ import SuperTest from "supertest";
3
+ import {PlatformTestingSdkOpts} from "../interfaces";
4
+
5
+ @Service()
6
+ @Scope(ProviderScope.REQUEST)
7
+ export class ScopeRequestService {
8
+ user: string;
9
+ }
10
+
11
+ @Controller("/scopes-request")
12
+ @Scope(ProviderScope.REQUEST)
13
+ export class ScopeRequestCtrl {
14
+ userId: string | null;
15
+
16
+ constructor(public scopeRequestService: ScopeRequestService) {}
17
+
18
+ $onDestroy() {
19
+ this.userId = null;
20
+ }
21
+
22
+ @Get("/:id")
23
+ async testPerRequest(@PathParams("id") userId: string): Promise<any> {
24
+ this.scopeRequestService.user = userId;
25
+ this.userId = userId;
26
+
27
+ return new Promise((resolve, reject) => {
28
+ if (userId === "0") {
29
+ setTimeout(() => {
30
+ resolve({userId, idSrv: this.scopeRequestService.user, idCtrl: this.userId});
31
+ }, 500);
32
+ }
33
+
34
+ if (userId === "1") {
35
+ setTimeout(() => {
36
+ resolve({userId, idSrv: this.scopeRequestService.user, idCtrl: this.userId});
37
+ }, 300);
38
+ }
39
+
40
+ if (userId === "2") {
41
+ setTimeout(() => {
42
+ resolve({userId, idSrv: this.scopeRequestService.user, idCtrl: this.userId});
43
+ }, 150);
44
+ }
45
+ });
46
+ }
47
+ }
48
+
49
+ export function testScopeRequest(options: PlatformTestingSdkOpts) {
50
+ let request: SuperTest.SuperTest<SuperTest.Test>;
51
+
52
+ beforeAll(
53
+ PlatformTest.bootstrap(options.server, {
54
+ ...options,
55
+ mount: {
56
+ "/rest": [ScopeRequestCtrl]
57
+ }
58
+ })
59
+ );
60
+ beforeAll(() => {
61
+ request = SuperTest(PlatformTest.callback());
62
+ });
63
+ afterAll(PlatformTest.reset);
64
+
65
+ describe("GET /rest/scopes-request/:id", () => {
66
+ const send = (id: string) =>
67
+ new Promise((resolve, reject) => {
68
+ request
69
+ .get(`/rest/scopes-request/${id}`)
70
+ .expect(200)
71
+ .end((err: any, response: any) => {
72
+ if (err) {
73
+ reject(err);
74
+ } else {
75
+ resolve({id, ...JSON.parse(response.text)});
76
+ }
77
+ });
78
+ });
79
+
80
+ it("should respond with the right userId per request", () => {
81
+ const promises = [];
82
+
83
+ promises.push(send("0"));
84
+ promises.push(send("1"));
85
+ promises.push(send("2"));
86
+
87
+ return Promise.all(promises).then((responses) => {
88
+ expect(responses).toEqual([
89
+ {
90
+ id: "0",
91
+ idCtrl: "0",
92
+ idSrv: "0",
93
+ userId: "0"
94
+ },
95
+ {
96
+ id: "1",
97
+ idCtrl: "1",
98
+ idSrv: "1",
99
+ userId: "1"
100
+ },
101
+ {
102
+ id: "2",
103
+ idCtrl: "2",
104
+ idSrv: "2",
105
+ userId: "2"
106
+ }
107
+ ]);
108
+ });
109
+ });
110
+ });
111
+ }
@@ -0,0 +1,104 @@
1
+ import {BodyParams, Constant, Controller, Get, PlatformTest, Post, Req, Session} from "@tsed/common";
2
+ import {NotFound} from "@tsed/exceptions";
3
+ import {Indexed, Unique} from "@tsed/mongoose";
4
+ import {Allow, Email, Ignore, MinLength, Property, Required, Returns} from "@tsed/schema";
5
+ import SuperTest from "supertest";
6
+ import {promisify} from "util";
7
+ import {PlatformTestingSdkOpts} from "../interfaces";
8
+
9
+ export class UserCreation {
10
+ @Property()
11
+ name: string;
12
+
13
+ @Indexed()
14
+ @Required()
15
+ @Email()
16
+ @Unique()
17
+ @Allow(null)
18
+ email: string;
19
+
20
+ @Required()
21
+ @MinLength(6)
22
+ @Allow(null)
23
+ password: string;
24
+ }
25
+
26
+ export class User extends UserCreation {
27
+ @Ignore()
28
+ declare password: string;
29
+ }
30
+
31
+ @Controller("/session")
32
+ export class SessionCtrl {
33
+ @Constant("PLATFORM_NAME")
34
+ platformName: string;
35
+
36
+ @Post("/connect")
37
+ async connect(@Session() session: any, @BodyParams() user: UserCreation) {
38
+ session.user = user;
39
+
40
+ return user;
41
+ }
42
+
43
+ @Get("/userinfo")
44
+ @Returns(200, User)
45
+ async userInfo(@Session("user") user: User): Promise<User> {
46
+ if (!user) {
47
+ throw new NotFound("User not found");
48
+ }
49
+
50
+ return user;
51
+ }
52
+
53
+ @Get("/logout")
54
+ @Returns(204)
55
+ async logout(@Req() req: any) {
56
+ switch (this.platformName) {
57
+ case "koa":
58
+ return (req.ctx.session = null);
59
+ case "express":
60
+ delete req.session.user;
61
+
62
+ return promisify(req.session.destroy.bind(req.session))();
63
+ }
64
+ }
65
+ }
66
+
67
+ export function testSession(options: PlatformTestingSdkOpts) {
68
+ let request: SuperTest.SuperTest<SuperTest.Test>;
69
+ beforeAll(
70
+ PlatformTest.bootstrap(options.server, {
71
+ ...options,
72
+ mount: {
73
+ "/rest": [SessionCtrl]
74
+ }
75
+ })
76
+ );
77
+ beforeAll(() => {
78
+ request = SuperTest.agent(PlatformTest.callback());
79
+ });
80
+ afterAll(PlatformTest.reset);
81
+
82
+ describe("Scenario1: POST /rest/session/connected", () => {
83
+ it("should keep connected user in session and destroy session", async () => {
84
+ await request.get("/rest/session/userinfo").expect(404);
85
+
86
+ await request.post("/rest/session/connect").send({
87
+ name: "name",
88
+ email: "test@test.fr",
89
+ password: "password1234"
90
+ });
91
+
92
+ // @ts-ignore
93
+ const response = await request.get("/rest/session/userinfo").expect(200);
94
+
95
+ expect(response.body).toEqual({
96
+ email: "test@test.fr",
97
+ name: "name"
98
+ });
99
+
100
+ await request.get("/rest/session/logout").expect(204);
101
+ await request.get("/rest/session/userinfo").expect(404);
102
+ });
103
+ });
104
+ }
@@ -0,0 +1,61 @@
1
+ import {Controller, PlatformTest} from "@tsed/common";
2
+ import {getValue} from "@tsed/core";
3
+ import {Configuration} from "@tsed/di";
4
+ import {PlatformRouter} from "@tsed/platform-router";
5
+ import {readFileSync} from "fs";
6
+ import SuperTest from "supertest";
7
+ import {PlatformTestingSdkOpts} from "../interfaces";
8
+
9
+ @Controller("/statics")
10
+ class CustomStaticsCtrl {
11
+ constructor(router: PlatformRouter, @Configuration() config: Configuration) {
12
+ router.statics("/", {
13
+ root: String(getValue(config, "statics./"))
14
+ });
15
+ }
16
+ }
17
+
18
+ export function testStatics(options: PlatformTestingSdkOpts) {
19
+ let request: SuperTest.SuperTest<SuperTest.Test>;
20
+ beforeEach(
21
+ PlatformTest.bootstrap(options.server, {
22
+ ...options,
23
+ mount: {
24
+ "/rest": [CustomStaticsCtrl]
25
+ }
26
+ })
27
+ );
28
+ beforeEach(() => {
29
+ request = SuperTest(PlatformTest.callback());
30
+ });
31
+ afterEach(PlatformTest.reset);
32
+
33
+ it("should return index HTML (1)", async () => {
34
+ const response = await request.get("/").expect(200);
35
+
36
+ expect(response.text).toEqual(readFileSync(`${options.rootDir}/public/index.html`, {encoding: "utf8"}));
37
+ });
38
+
39
+ it("should return index HTML (2)", async () => {
40
+ const response = await request.get("/index.html").expect(200);
41
+
42
+ expect(response.text).toEqual(readFileSync(`${options.rootDir}/public/index.html`, {encoding: "utf8"}));
43
+ });
44
+
45
+ it("should return index HTML (3)", async () => {
46
+ const response = await request.get("/rest/statics/index.html").expect(200);
47
+
48
+ expect(response.text).toEqual(readFileSync(`${options.rootDir}/public/index.html`, {encoding: "utf8"}));
49
+ });
50
+
51
+ it("should return 404", async () => {
52
+ const response = await request.get("/index-te.html").expect(404);
53
+
54
+ expect(response.body).toEqual({
55
+ errors: [],
56
+ message: 'Resource "/index-te.html" not found',
57
+ name: "NOT_FOUND",
58
+ status: 404
59
+ });
60
+ });
61
+ }
@@ -0,0 +1,68 @@
1
+ import {Controller, Get, Locals, Middleware, PlatformTest, UseBefore} from "@tsed/common";
2
+ import {EOL} from "os";
3
+ import SuperTest from "supertest";
4
+ import {View} from "@tsed/platform-views";
5
+ import {PlatformTestingSdkOpts} from "../interfaces";
6
+
7
+ @Middleware()
8
+ class LocalsMiddleware {
9
+ use(@Locals() locals: any) {
10
+ locals.localID = "local-10909";
11
+ }
12
+ }
13
+
14
+ @Controller("/views")
15
+ class ViewCtrl {
16
+ @Get("/scenario-1")
17
+ @UseBefore(LocalsMiddleware)
18
+ @View("view.ejs", {opts: "opts"})
19
+ testScenario1() {
20
+ return {
21
+ world: "world"
22
+ };
23
+ }
24
+
25
+ @Get("/scenario-2")
26
+ @View("view", {opts: "opts"}) // missing extension
27
+ testScenario2() {
28
+ return {
29
+ world: "world"
30
+ };
31
+ }
32
+ }
33
+
34
+ export function testView(options: PlatformTestingSdkOpts) {
35
+ let request: SuperTest.SuperTest<SuperTest.Test>;
36
+
37
+ beforeAll(
38
+ PlatformTest.bootstrap(options.server, {
39
+ ...options,
40
+ mount: {
41
+ "/rest": [ViewCtrl]
42
+ }
43
+ })
44
+ );
45
+ beforeAll(() => {
46
+ request = SuperTest(PlatformTest.callback());
47
+ });
48
+ afterAll(PlatformTest.reset);
49
+
50
+ describe("Scenario1: GET /rest/views/scenario-1", () => {
51
+ it("should render a view", async () => {
52
+ const response = await request.get("/rest/views/scenario-1").expect(200);
53
+
54
+ expect(response.headers["content-type"]).toEqual("text/html; charset=utf-8");
55
+ expect(response.text).toEqual(`<p>Hello world with opts and ID local-10909</p>${EOL}`);
56
+ });
57
+ });
58
+
59
+ describe("Scenario2: GET /rest/views/scenario-2", () => {
60
+ it("should throw an error when extension is not defined", async () => {
61
+ const response = await request.get("/rest/views/scenario-2").expect(500);
62
+
63
+ expect(response.body.name).toEqual("TEMPLATE_RENDER_ERROR");
64
+ expect(response.body.message).toContain("Template rendering error: ViewCtrl.testScenario2()");
65
+ expect(response.body.status).toEqual(500);
66
+ });
67
+ });
68
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "extends": "./tsconfig.compile.json",
3
+ "compilerOptions": {
4
+ "baseUrl": ".",
5
+ "module": "ES2020",
6
+ "rootDir": "src",
7
+ "outDir": "./lib/esm",
8
+ "declaration": true,
9
+ "declarationDir": "./lib/types"
10
+ },
11
+ "exclude": ["node_modules", "test", "lib", "benchmark", "coverage", "spec", "**/*.benchmark.ts", "**/*.spec.ts", "keys", "jest.config.js"]
12
+ }