Skip to content

Commit

Permalink
chore: Add complementary integration test on swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Jan 27, 2020
1 parent 0dad57d commit 6d9e7af
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 0 deletions.
40 changes: 40 additions & 0 deletions packages/swagger/test/helpers/Server.ts
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 packages/swagger/test/helpers/controllers/CalendarsController.ts
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"})
];
}
}
15 changes: 15 additions & 0 deletions packages/swagger/test/helpers/models/Calendar.ts
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;
}

}
114 changes: 114 additions & 0 deletions packages/swagger/test/swagger.integration.spec.ts
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": {}
});
});
});

0 comments on commit 6d9e7af

Please sign in to comment.