-
-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add complementary integration test on swagger
- Loading branch information
Showing
4 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {ServerLoader, ServerSettings} from "@tsed/common"; | ||
import "@tsed/swagger"; | ||
import * as Path from "path"; | ||
|
||
const cookieParser = require("cookie-parser"), | ||
bodyParser = require("body-parser"), | ||
compress = require("compression"), | ||
methodOverride = require("method-override"); | ||
|
||
const rootDir = Path.resolve(__dirname); | ||
|
||
@ServerSettings({ | ||
rootDir, | ||
port: 8001, | ||
httpsPort: false, | ||
logger: { | ||
level: "info", | ||
logRequest: true | ||
}, | ||
swagger: [ | ||
{ | ||
path: "/api-doc", | ||
showExplorer: true | ||
} | ||
] | ||
}) | ||
export class Server extends ServerLoader { | ||
public $beforeRoutesInit(): void { | ||
this | ||
.use(bodyParser.json()) | ||
.use( | ||
bodyParser.urlencoded({ | ||
extended: true | ||
}) | ||
) | ||
.use(cookieParser()) | ||
.use(compress({})) | ||
.use(methodOverride()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/swagger/test/helpers/controllers/CalendarsController.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {Controller, Get} from "@tsed/common"; | ||
import {PathParams} from "@tsed/common/src/mvc/decorators/params/pathParams"; | ||
import {Returns} from "../../../src/decorators/returns"; | ||
import {ReturnsArray} from "../../../src/decorators/returnsArray"; | ||
import {Calendar} from "../models/Calendar"; | ||
|
||
@Controller("/calendars") | ||
export class CalendarsController { | ||
@Get("/:id") | ||
@Returns(200, {type: Calendar}) | ||
async get(@PathParams("id") id: string): Promise<Calendar> { | ||
return new Calendar({id, name: "test"}); | ||
} | ||
|
||
@Get("/") | ||
@ReturnsArray(200, {type: Calendar}) | ||
async getAll(): Promise<Calendar[]> { | ||
return [ | ||
new Calendar({id: 1, name: "name"}), | ||
new Calendar({id: 2, name: "name"}) | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {Property, Required} from "@tsed/common"; | ||
|
||
export class Calendar { | ||
@Property("id") | ||
id: string; | ||
|
||
@Required() | ||
name: string; | ||
|
||
constructor({id, name}: any = {}) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import {ExpressApplication} from "@tsed/common/src"; | ||
import {inject, TestContext} from "@tsed/testing/src"; | ||
import {expect} from "chai"; | ||
import * as SuperTest from "supertest"; | ||
import {Server} from "./helpers/Server"; | ||
|
||
describe("Swagger integration", () => { | ||
let request: SuperTest.SuperTest<SuperTest.Test>; | ||
beforeEach(TestContext.bootstrap(Server)); | ||
beforeEach( | ||
inject([ExpressApplication], (expressApplication: ExpressApplication) => { | ||
request = SuperTest(expressApplication); | ||
}) | ||
); | ||
afterEach(TestContext.reset); | ||
|
||
it("should swagger spec", async () => { | ||
const response = await request.get("/api-doc/swagger.json").expect(200); | ||
const result = await request.get("/rest/calendars").expect(200); | ||
|
||
expect(result.body).to.deep.eq([ | ||
{ | ||
"id": 1, | ||
"name": "name" | ||
}, | ||
{ | ||
"id": 2, | ||
"name": "name" | ||
} | ||
]); | ||
expect(response.body).to.deep.eq({ | ||
"swagger": "2.0", | ||
"tags": [ | ||
{ | ||
"name": "CalendarsController" | ||
} | ||
], | ||
"consumes": [ | ||
"application/json" | ||
], | ||
"definitions": { | ||
"Calendar": { | ||
"properties": { | ||
"id": { | ||
"type": "string" | ||
}, | ||
"name": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"name" | ||
], | ||
"type": "object" | ||
} | ||
}, | ||
"info": { | ||
"description": "", | ||
"termsOfService": "", | ||
"title": "Api documentation", | ||
"version": "1.0.0" | ||
}, | ||
"paths": { | ||
"/rest/calendars": { | ||
"get": { | ||
"operationId": "CalendarsController.getAll", | ||
"responses": { | ||
"200": { | ||
"description": "Success", | ||
"schema": { | ||
"items": { | ||
"$ref": "#/definitions/Calendar" | ||
}, | ||
"type": "array" | ||
} | ||
} | ||
}, | ||
"tags": [ | ||
"CalendarsController" | ||
] | ||
} | ||
}, | ||
"/rest/calendars/{id}": { | ||
"get": { | ||
"operationId": "CalendarsController.get", | ||
"parameters": [ | ||
{ | ||
"in": "path", | ||
"name": "id", | ||
"required": true, | ||
"type": "string" | ||
} | ||
], | ||
"responses": { | ||
"200": { | ||
"description": "Success", | ||
"schema": { | ||
"$ref": "#/definitions/Calendar" | ||
} | ||
} | ||
}, | ||
"tags": [ | ||
"CalendarsController" | ||
] | ||
} | ||
} | ||
}, | ||
"produces": [ | ||
"application/json" | ||
], | ||
"securityDefinitions": {} | ||
}); | ||
}); | ||
}); |