@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,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
+ }