/
testAcceptMime.ts
56 lines (51 loc) · 1.59 KB
/
testAcceptMime.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import {AcceptMime, Controller, HeaderParams, PlatformTest, Post} from "@tsed/common";
import {expect} from "chai";
import * as SuperTest from "supertest";
import {PlatformTestOptions} from "../interfaces";
@Controller("/accept-mime")
class TestAcceptMimeCtrl {
@Post("/scenario-1")
@AcceptMime("application/json")
scenario1(@HeaderParams("Accept") accept: string) {
return {
accept
};
}
}
export function testAcceptMime(options: PlatformTestOptions) {
let request: SuperTest.SuperTest<SuperTest.Test>;
before(
PlatformTest.bootstrap(options.server, {
...options,
mount: {
"/rest": [TestAcceptMimeCtrl]
}
})
);
before(() => {
request = SuperTest(PlatformTest.callback());
});
after(PlatformTest.reset);
describe("Scenario 1: POST /rest/accept-mime/scenario-1", () => {
it('should return a 200 response when Accept header match with @AcceptMime("application/json")', async () => {
const response = await request
.post("/rest/accept-mime/scenario-1")
.set({
Accept: "application/json"
})
.expect(200);
expect(response.body).to.deep.equal({
accept: "application/json"
});
});
it('should return a 406 response when Accept header doesn\'t match with @AcceptMime("application/json")', async () => {
const response = await request
.post("/rest/accept-mime/scenario-1")
.set({
Accept: "application/xml"
})
.expect(406);
expect(response.text).to.deep.equal("You must accept content-type application/json");
});
});
}