-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.spec.ts
84 lines (83 loc) · 3.46 KB
/
handler.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
84
import {APIGatewayEventRequestContext, APIGatewayProxyEvent, APIGatewayProxyResult, Context} from "aws-lambda";
import {addOriginResponseHeader, http} from "./handler";
describe("Handler", () => {
it("should return a positive response for index.html on root", async () => {
const response = await http({path: "/index.html"} as APIGatewayProxyEvent);
expect(response.statusCode).toBe(200);
});
it("should return a positive response for index.html on subpath", async () => {
const response = await http({path: "/aws-lambda-app/index.html"} as APIGatewayProxyEvent);
expect(response.statusCode).toBe(200);
});
it("should return a 404 for an unknown path", async () => {
const response = await http({path: "/unknown-path"} as APIGatewayProxyEvent);
expect(response.statusCode).toBe(404);
});
it("should add CORS response", async () => {
const response = await http({
path: "/index.html",
headers: {origin: "xyz"},
body: null,
httpMethod: "GET",
isBase64Encoded: false,
multiValueHeaders: {},
multiValueQueryStringParameters: {},
pathParameters: {},
queryStringParameters: {},
requestContext: {} as APIGatewayEventRequestContext,
resource: "",
stageVariables: {},
} as APIGatewayProxyEvent);
expect(response.statusCode).toBe(200);
expect(response.headers).toEqual({
"Content-Type": "text/html",
"Access-Control-Allow-Origin": "xyz",
});
});
it("should not add CORS response when there are no headers in event", async () => {
const response = {} as APIGatewayProxyResult;
await addOriginResponseHeader({} as APIGatewayProxyEvent, response);
expect(response.headers).not.toEqual({
"Content-Type": "text/html",
"Access-Control-Allow-Origin": "xyz",
});
});
it("should add CORS response when there is an origin header in event but not in response", async () => {
const response = {} as APIGatewayProxyResult;
await addOriginResponseHeader({
path: "/index.html",
headers: {origin: "xyz"},
body: null,
httpMethod: "GET",
isBase64Encoded: false,
multiValueHeaders: {},
multiValueQueryStringParameters: {},
pathParameters: {},
queryStringParameters: {},
requestContext: {} as APIGatewayEventRequestContext,
resource: "",
stageVariables: {},
} as APIGatewayProxyEvent, response);
expect(response.headers).toEqual({
"Access-Control-Allow-Origin": "xyz",
});
});
it("should not add CORS response when there is no origin header in event but not in response", async () => {
const response = {} as APIGatewayProxyResult;
await addOriginResponseHeader({
path: "/index.html",
headers: {},
body: null,
httpMethod: "GET",
isBase64Encoded: false,
multiValueHeaders: {},
multiValueQueryStringParameters: {},
pathParameters: {},
queryStringParameters: {},
requestContext: {} as APIGatewayEventRequestContext,
resource: "",
stageVariables: {},
} as APIGatewayProxyEvent, response);
expect(response.headers).toBeUndefined();
});
});