-
-
Notifications
You must be signed in to change notification settings - Fork 281
/
setResponseHeaders.spec.ts
83 lines (64 loc) · 2.15 KB
/
setResponseHeaders.spec.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import {EndpointMetadata, Get, Redirect, Returns} from "@tsed/schema";
import {PlatformTest} from "../services/PlatformTest";
import {setResponseHeaders} from "./setResponseHeaders";
describe("setResponseHeaders", () => {
beforeEach(() => PlatformTest.create());
afterEach(() => PlatformTest.reset());
it("should set headers, status and contentType", async () => {
class Test {
@Get("/")
@Returns(200).Header("x-header", "test")
test() {}
}
const ctx = PlatformTest.createRequestContext();
ctx.endpoint = EndpointMetadata.get(Test, "test");
// WHEN
await setResponseHeaders(ctx);
// THEN
expect(ctx.response.getHeaders()).toEqual({"x-request-id": "id", "x-header": "test"});
expect(ctx.response.statusCode).toEqual(200);
});
it("should not set headers automatically if the value isn't declared", async () => {
class Test {
@Get("/")
@Returns(200).Header("x-header")
test() {}
}
const ctx = PlatformTest.createRequestContext();
ctx.endpoint = EndpointMetadata.get(Test, "test");
// WHEN
await setResponseHeaders(ctx);
// THEN
expect(ctx.response.getHeaders()).toEqual({"x-request-id": "id"});
expect(ctx.response.statusCode).toEqual(200);
});
it("should redirect", async () => {
class Test {
@Get("/")
@Redirect(301, "/path")
test() {}
}
const ctx = PlatformTest.createRequestContext();
ctx.endpoint = EndpointMetadata.get(Test, "test");
jest.spyOn(ctx.response, "redirect").mockReturnValue(undefined as any);
// WHEN
await setResponseHeaders(ctx);
// THEN
expect(ctx.response.redirect).toBeCalledWith(301, "/path");
});
it("should do nothing when headers is already sent", async () => {
class Test {
@Get("/")
@Returns(200).Header("x-header", "test")
test() {}
}
const ctx = PlatformTest.createRequestContext();
ctx.response.raw.headersSent = true;
ctx.endpoint = EndpointMetadata.get(Test, "test");
jest.spyOn(ctx.response.raw, "set");
// WHEN
await setResponseHeaders(ctx);
// THEN
return expect(ctx.response.raw.set).not.toBeCalled();
});
});