@tsed/platform-test-sdk 8.0.1 → 8.0.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.
Files changed (42) hide show
  1. package/package.json +15 -14
  2. package/src/components/PlatformTestSdk.ts +30 -0
  3. package/src/data/file.txt +1 -0
  4. package/src/data/response.data.json +3 -0
  5. package/src/data/swagger.json +32 -0
  6. package/src/errors/CustomBadRequest.ts +16 -0
  7. package/src/errors/CustomInternalError.ts +16 -0
  8. package/src/index.ts +40 -0
  9. package/src/interfaces/PlatformTestingSdkOpts.ts +10 -0
  10. package/src/interfaces/index.ts +4 -0
  11. package/src/modules/feature/FeatureModule.ts +10 -0
  12. package/src/modules/feature/controllers/FeatureController.ts +11 -0
  13. package/src/tests/exports.ts +71 -0
  14. package/src/tests/testAcceptMime.ts +99 -0
  15. package/src/tests/testAuth.ts +345 -0
  16. package/src/tests/testBodyParams.ts +488 -0
  17. package/src/tests/testCache.ts +283 -0
  18. package/src/tests/testChildrenControllers.ts +59 -0
  19. package/src/tests/testCookies.ts +41 -0
  20. package/src/tests/testCustom404.ts +35 -0
  21. package/src/tests/testDeepQueryParams.ts +125 -0
  22. package/src/tests/testErrors.ts +233 -0
  23. package/src/tests/testHandlers.ts +94 -0
  24. package/src/tests/testHeaderParams.ts +158 -0
  25. package/src/tests/testHeaders.ts +78 -0
  26. package/src/tests/testInheritanceController.ts +206 -0
  27. package/src/tests/testLocals.ts +53 -0
  28. package/src/tests/testLocation.ts +40 -0
  29. package/src/tests/testMiddlewares.ts +155 -0
  30. package/src/tests/testModule.ts +50 -0
  31. package/src/tests/testMulter.ts +158 -0
  32. package/src/tests/testPathParams.ts +127 -0
  33. package/src/tests/testQueryParams.ts +238 -0
  34. package/src/tests/testRedirect.ts +129 -0
  35. package/src/tests/testResponse.ts +533 -0
  36. package/src/tests/testResponseFilter.ts +106 -0
  37. package/src/tests/testRouting.ts +71 -0
  38. package/src/tests/testScopeRequest.ts +116 -0
  39. package/src/tests/testSession.ts +108 -0
  40. package/src/tests/testStatics.ts +63 -0
  41. package/src/tests/testStream.ts +62 -0
  42. package/src/tests/testView.ts +74 -0
package/package.json CHANGED
@@ -2,13 +2,14 @@
2
2
  "name": "@tsed/platform-test-sdk",
3
3
  "description": "Package to test platform adapter integration with Ts.ED",
4
4
  "type": "module",
5
- "version": "8.0.1",
5
+ "version": "8.0.3",
6
6
  "source": "./src/index.ts",
7
7
  "main": "./lib/esm/index.js",
8
8
  "module": "./lib/esm/index.js",
9
9
  "typings": "./lib/types/index.d.ts",
10
10
  "exports": {
11
11
  ".": {
12
+ "@tsed/source": "./src/index.ts",
12
13
  "types": "./lib/types/index.d.ts",
13
14
  "import": "./lib/esm/index.js",
14
15
  "default": "./lib/esm/index.js"
@@ -27,25 +28,25 @@
27
28
  "Adapter"
28
29
  ],
29
30
  "dependencies": {
30
- "@tsed/ajv": "8.0.1",
31
- "@tsed/core": "8.0.1",
32
- "@tsed/di": "8.0.1",
33
- "@tsed/json-mapper": "8.0.1",
31
+ "@tsed/ajv": "8.0.3",
32
+ "@tsed/core": "8.0.3",
33
+ "@tsed/di": "8.0.3",
34
+ "@tsed/json-mapper": "8.0.3",
34
35
  "@tsed/logger": "6.7.8",
35
- "@tsed/platform-cache": "8.0.1",
36
- "@tsed/platform-http": "8.0.1",
37
- "@tsed/platform-params": "8.0.1",
38
- "@tsed/platform-router": "8.0.1",
39
- "@tsed/platform-views": "8.0.1",
40
- "@tsed/schema": "8.0.1",
41
- "@tsed/swagger": "8.0.1",
36
+ "@tsed/platform-cache": "8.0.3",
37
+ "@tsed/platform-http": "8.0.3",
38
+ "@tsed/platform-params": "8.0.3",
39
+ "@tsed/platform-router": "8.0.3",
40
+ "@tsed/platform-views": "8.0.3",
41
+ "@tsed/schema": "8.0.3",
42
+ "@tsed/swagger": "8.0.3",
42
43
  "ajv": "^8.17.1",
43
44
  "axios": "1.7.7",
44
45
  "read-pkg-up": "7.0.1"
45
46
  },
46
47
  "devDependencies": {
47
- "@tsed/barrels": "8.0.1",
48
- "@tsed/typescript": "8.0.1",
48
+ "@tsed/barrels": "8.0.3",
49
+ "@tsed/typescript": "8.0.3",
49
50
  "eslint": "9.12.0",
50
51
  "typescript": "5.4.5"
51
52
  },
@@ -0,0 +1,30 @@
1
+ import {PlatformTest} from "@tsed/platform-http/testing";
2
+
3
+ import {PlatformTestingSdkOpts} from "../interfaces/index.js";
4
+ import {specsContainer} from "../tests/exports.js";
5
+
6
+ export class PlatformTestSdk {
7
+ protected constructor(private options: PlatformTestingSdkOpts) {}
8
+
9
+ static create(options: PlatformTestingSdkOpts) {
10
+ return new PlatformTestSdk(options);
11
+ }
12
+
13
+ test(name: string, options: any = {}) {
14
+ specsContainer.get(name)!({
15
+ ...this.options,
16
+ ...options
17
+ });
18
+ }
19
+
20
+ bootstrap(options: any = {}) {
21
+ return PlatformTest.bootstrap(this.options.server, {
22
+ ...this.options,
23
+ ...options
24
+ });
25
+ }
26
+
27
+ reset() {
28
+ return PlatformTest.reset();
29
+ }
30
+ }
@@ -0,0 +1 @@
1
+ test content
@@ -0,0 +1,3 @@
1
+ {
2
+ "id": "1"
3
+ }
@@ -0,0 +1,32 @@
1
+ {
2
+ "openapi": "3.0.1",
3
+ "info": {
4
+ "version": "1.0.0",
5
+ "description": "Swagger description",
6
+ "title": "Swagger Title",
7
+ "termsOfService": "http://swagger.io/terms/",
8
+ "contact": {
9
+ "email": "apiteam@swagger.io"
10
+ },
11
+ "license": {
12
+ "name": "Apache 2.0",
13
+ "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
14
+ }
15
+ },
16
+ "components": {
17
+ "securitySchemes": {
18
+ "global_auth": {
19
+ "type": "oauth2",
20
+ "flows": {
21
+ "implicit": {
22
+ "authorizationUrl": "http://petstore.swagger.io/oauth/dialog",
23
+ "scopes": {
24
+ "admin": "Admin access"
25
+ }
26
+ }
27
+ }
28
+ }
29
+ }
30
+ },
31
+ "paths": {}
32
+ }
@@ -0,0 +1,16 @@
1
+ import {BadRequest} from "@tsed/exceptions";
2
+ import {ResponseErrorObject} from "@tsed/platform-http";
3
+
4
+ export class CustomBadRequest extends BadRequest implements ResponseErrorObject {
5
+ name = "CUSTOM_BAD_REQUEST";
6
+ errors: any[];
7
+ headers = {};
8
+
9
+ constructor(message: string) {
10
+ super(message);
11
+ this.errors = ["test"];
12
+ this.headers = {
13
+ "X-HEADER-ERROR": "deny"
14
+ };
15
+ }
16
+ }
@@ -0,0 +1,16 @@
1
+ import {InternalServerError} from "@tsed/exceptions";
2
+ import {ResponseErrorObject} from "@tsed/platform-http";
3
+
4
+ export class CustomInternalError extends InternalServerError implements ResponseErrorObject {
5
+ name = "CUSTOM_INTERNAL_SERVER_ERROR";
6
+ errors: any[];
7
+ headers = {};
8
+
9
+ constructor(message: string) {
10
+ super(message);
11
+ this.errors = ["test"];
12
+ this.headers = {
13
+ "X-HEADER-ERROR": "deny"
14
+ };
15
+ }
16
+ }
package/src/index.ts ADDED
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @file Automatically generated by @tsed/barrels.
3
+ */
4
+ export * from "./components/PlatformTestSdk.js";
5
+ export * from "./errors/CustomBadRequest.js";
6
+ export * from "./errors/CustomInternalError.js";
7
+ export * from "./interfaces/index.js";
8
+ export * from "./interfaces/PlatformTestingSdkOpts.js";
9
+ export * from "./modules/feature/controllers/FeatureController.js";
10
+ export * from "./modules/feature/FeatureModule.js";
11
+ export * from "./tests/exports.js";
12
+ export * from "./tests/testAcceptMime.js";
13
+ export * from "./tests/testAuth.js";
14
+ export * from "./tests/testBodyParams.js";
15
+ export * from "./tests/testCache.js";
16
+ export * from "./tests/testChildrenControllers.js";
17
+ export * from "./tests/testCookies.js";
18
+ export * from "./tests/testCustom404.js";
19
+ export * from "./tests/testDeepQueryParams.js";
20
+ export * from "./tests/testErrors.js";
21
+ export * from "./tests/testHandlers.js";
22
+ export * from "./tests/testHeaderParams.js";
23
+ export * from "./tests/testHeaders.js";
24
+ export * from "./tests/testInheritanceController.js";
25
+ export * from "./tests/testLocals.js";
26
+ export * from "./tests/testLocation.js";
27
+ export * from "./tests/testMiddlewares.js";
28
+ export * from "./tests/testModule.js";
29
+ export * from "./tests/testMulter.js";
30
+ export * from "./tests/testPathParams.js";
31
+ export * from "./tests/testQueryParams.js";
32
+ export * from "./tests/testRedirect.js";
33
+ export * from "./tests/testResponse.js";
34
+ export * from "./tests/testResponseFilter.js";
35
+ export * from "./tests/testRouting.js";
36
+ export * from "./tests/testScopeRequest.js";
37
+ export * from "./tests/testSession.js";
38
+ export * from "./tests/testStatics.js";
39
+ export * from "./tests/testStream.js";
40
+ export * from "./tests/testView.js";
@@ -0,0 +1,10 @@
1
+ import {Type} from "@tsed/core";
2
+ import {PlatformAdapter} from "@tsed/platform-http";
3
+
4
+ export interface PlatformTestingSdkOpts {
5
+ rootDir: string;
6
+ adapter: Type<PlatformAdapter<any>>;
7
+ server: Type<any>;
8
+
9
+ [key: string]: any;
10
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * @file Automatically generated by @tsed/barrels.
3
+ */
4
+ export * from "./PlatformTestingSdkOpts.js";
@@ -0,0 +1,10 @@
1
+ import {Module} from "@tsed/di";
2
+
3
+ import {FeatureController} from "./controllers/FeatureController.js";
4
+
5
+ @Module({
6
+ mount: {
7
+ "/rest": [FeatureController]
8
+ }
9
+ })
10
+ export class FeatureModule {}
@@ -0,0 +1,11 @@
1
+ import {Controller} from "@tsed/di";
2
+ import {Get, Hidden} from "@tsed/schema";
3
+
4
+ @Hidden()
5
+ @Controller("/features")
6
+ export class FeatureController {
7
+ @Get("/")
8
+ get() {
9
+ return "From feature";
10
+ }
11
+ }
@@ -0,0 +1,71 @@
1
+ import {PlatformTestingSdkOpts} from "../interfaces/index.js";
2
+ import {testAcceptMime} from "./testAcceptMime.js";
3
+ import {testAuth} from "./testAuth.js";
4
+ import {testBodyParams} from "./testBodyParams.js";
5
+ import {testCache} from "./testCache.js";
6
+ import {testChildrenControllers} from "./testChildrenControllers.js";
7
+ import {testCookies} from "./testCookies.js";
8
+ import {testCustom404} from "./testCustom404.js";
9
+ import {testDeepQueryParams} from "./testDeepQueryParams.js";
10
+ import {testErrors} from "./testErrors.js";
11
+ import {testHandlers} from "./testHandlers.js";
12
+ import {testHeaderParams} from "./testHeaderParams.js";
13
+ import {testHeaders} from "./testHeaders.js";
14
+ import {testInheritanceController} from "./testInheritanceController.js";
15
+ import {testLocals} from "./testLocals.js";
16
+ import {testLocation} from "./testLocation.js";
17
+ import {testMiddlewares} from "./testMiddlewares.js";
18
+ import {testModule} from "./testModule.js";
19
+ import {testMulter} from "./testMulter.js";
20
+ import {testPathParams} from "./testPathParams.js";
21
+ import {testQueryParams} from "./testQueryParams.js";
22
+ import {testRedirect} from "./testRedirect.js";
23
+ import {testResponse} from "./testResponse.js";
24
+ import {testResponseFilter} from "./testResponseFilter.js";
25
+ import {testRouting} from "./testRouting.js";
26
+ import {testScopeRequest} from "./testScopeRequest.js";
27
+ import {testSession} from "./testSession.js";
28
+ import {testStatics} from "./testStatics.js";
29
+ import {testStream} from "./testStream.js";
30
+ import {testView} from "./testView.js";
31
+
32
+ export const specsContainer = new Map<string, (options: PlatformTestingSdkOpts) => void>();
33
+
34
+ // HANDLERS AND MIDDLEWARES
35
+ specsContainer.set("handlers", testHandlers);
36
+ specsContainer.set("auth", testAuth);
37
+ specsContainer.set("middlewares", testMiddlewares);
38
+
39
+ // RESPONSES
40
+ specsContainer.set("headers", testHeaders);
41
+ specsContainer.set("acceptMime", testAcceptMime);
42
+ specsContainer.set("location", testLocation);
43
+ specsContainer.set("redirect", testRedirect);
44
+ specsContainer.set("view", testView);
45
+ specsContainer.set("errors", testErrors);
46
+ specsContainer.set("response", testResponse);
47
+ specsContainer.set("stream", testStream);
48
+ specsContainer.set("responseFilter", testResponseFilter);
49
+ specsContainer.set("custom404", testCustom404);
50
+ specsContainer.set("statics", testStatics);
51
+ specsContainer.set("cache", testCache);
52
+
53
+ // INPUTS
54
+ specsContainer.set("cookies", testCookies);
55
+ specsContainer.set("session", testSession);
56
+ specsContainer.set("headerParams", testHeaderParams);
57
+ specsContainer.set("bodyParams", testBodyParams);
58
+ specsContainer.set("pathParams", testPathParams);
59
+ specsContainer.set("queryParams", testQueryParams);
60
+ specsContainer.set("deepQueryParams", testDeepQueryParams);
61
+ specsContainer.set("locals", testLocals);
62
+ specsContainer.set("multer", testMulter);
63
+
64
+ // ROUTING
65
+ specsContainer.set("scopeRequest", testScopeRequest);
66
+ specsContainer.set("routing", testRouting);
67
+
68
+ // CODE ORGANIZATION
69
+ specsContainer.set("childrenControllers", testChildrenControllers);
70
+ specsContainer.set("inheritanceController", testInheritanceController);
71
+ specsContainer.set("module", testModule);
@@ -0,0 +1,99 @@
1
+ import {Controller} from "@tsed/di";
2
+ import {PlatformTest} from "@tsed/platform-http/testing";
3
+ import {HeaderParams} from "@tsed/platform-params";
4
+ import {AcceptMime, ContentType, Get, Post} from "@tsed/schema";
5
+ import SuperTest from "supertest";
6
+ import {afterAll, beforeAll, describe, expect, it} from "vitest";
7
+
8
+ import {PlatformTestingSdkOpts} from "../interfaces/index.js";
9
+
10
+ @Controller("/accept-mime")
11
+ class TestAcceptMimeCtrl {
12
+ @Post("/scenario-1")
13
+ @AcceptMime("application/json")
14
+ scenario1(@HeaderParams("Accept") accept: string) {
15
+ return {
16
+ accept
17
+ };
18
+ }
19
+
20
+ @Get("/scenario-2")
21
+ @ContentType("text")
22
+ @AcceptMime("text")
23
+ public ping() {
24
+ return "pong";
25
+ }
26
+ }
27
+
28
+ export function testAcceptMime(options: PlatformTestingSdkOpts) {
29
+ let request: SuperTest.Agent;
30
+
31
+ beforeAll(
32
+ PlatformTest.bootstrap(options.server, {
33
+ ...options,
34
+ mount: {
35
+ "/rest": [TestAcceptMimeCtrl]
36
+ }
37
+ })
38
+ );
39
+ beforeAll(() => {
40
+ request = SuperTest(PlatformTest.callback());
41
+ });
42
+ afterAll(PlatformTest.reset);
43
+ describe("Scenario 1: POST /rest/accept-mime/scenario-1", () => {
44
+ it('should return a 200 response when Accept header match with @AcceptMime("application/json")', async () => {
45
+ const response = await request
46
+ .post("/rest/accept-mime/scenario-1")
47
+ .set({
48
+ Accept: "application/json"
49
+ })
50
+ .expect(200);
51
+
52
+ expect(response.body).toEqual({
53
+ accept: "application/json"
54
+ });
55
+ });
56
+ it('should return a 406 response when Accept header doesn\'t match with @AcceptMime("application/json")', async () => {
57
+ const response = await request
58
+ .post("/rest/accept-mime/scenario-1")
59
+ .set({
60
+ Accept: "application/xml"
61
+ })
62
+ .expect(406);
63
+
64
+ expect(response.body).toEqual({
65
+ name: "NOT_ACCEPTABLE",
66
+ message: "You must accept content-type application/json",
67
+ status: 406,
68
+ errors: []
69
+ });
70
+ });
71
+ });
72
+ describe("Scenario 2: GET /rest/accept-mime/scenario-2", () => {
73
+ it('should return a 200 response when Accept header match with @AcceptMime("text")', async () => {
74
+ const response = await request
75
+ .get("/rest/accept-mime/scenario-2")
76
+ .set({
77
+ Accept: "text/*, application/json"
78
+ })
79
+ .expect(200);
80
+
81
+ expect(response.text).toEqual("pong");
82
+ });
83
+ it('should return a 406 response when Accept header doesn\'t match with @AcceptMime("text")', async () => {
84
+ const response = await request
85
+ .get("/rest/accept-mime/scenario-2")
86
+ .set({
87
+ Accept: "application/xml"
88
+ })
89
+ .expect(406);
90
+
91
+ expect(response.body).toEqual({
92
+ name: "NOT_ACCEPTABLE",
93
+ message: "You must accept content-type text",
94
+ status: 406,
95
+ errors: []
96
+ });
97
+ });
98
+ });
99
+ }