-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintegration.deferred.test.ts
More file actions
102 lines (96 loc) · 2.62 KB
/
Copy pathintegration.deferred.test.ts
File metadata and controls
102 lines (96 loc) · 2.62 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import app from "./app"
import lambda from "../src/lambda"
const testEnvironment = () => {
let __test = 0
const _handler = lambda.deferred(
() =>
new Promise((resolve) => {
__test = __test + 1
setTimeout(() => {
resolve(app)
}, 10)
})
)
return {
getValue: () => __test,
handler: _handler,
}
}
describe("integration for deferred app", () => {
it("returns static file", async () => {
const myEvent = {
path: "/static/file.png",
httpMethod: "GET",
multiValueHeaders: {},
queryStringParameters: {},
isBase64Encoded: false,
body: null,
}
const t = testEnvironment()
const response = await t.handler(myEvent)
expect(response.statusCode).toEqual(200)
expect(response.isBase64Encoded).toEqual(true)
expect(response.multiValueHeaders!["content-type"][0]).toEqual("image/png")
expect(response.multiValueHeaders!["content-length"][0]).toEqual("178")
})
it("resolves the app promise only once", async () => {
const myEvent = {
path: "/static/file.png",
httpMethod: "GET",
multiValueHeaders: {},
queryStringParameters: {},
isBase64Encoded: false,
body: null,
}
const t = testEnvironment()
await t.handler(myEvent)
expect(t.getValue()).toEqual(1)
await t.handler(myEvent)
expect(t.getValue()).toEqual(1)
})
it("handler returns rejected promise if app cannot be initialized", async () => {
const failingHandler = lambda.deferred(
() => Promise.reject(new Error("failed to initialize app")),
{ onError: async () => undefined }
)
const myEvent = {
path: "/static/file.png",
httpMethod: "GET",
multiValueHeaders: {},
queryStringParameters: {},
isBase64Encoded: false,
body: null,
}
try {
await failingHandler(myEvent)
fail(new Error("should have failed"))
} catch (e) {
const err = e as Error
expect(err.message).toEqual("failed to initialize app")
}
})
it("returns 500 if there is a problem with the request", async () => {
const failingApp = lambda.deferred(
() =>
Promise.resolve(() => {
throw new Error("failed")
}),
{ onError: async () => undefined }
)
const myEvent = {
path: "/static/file.png",
httpMethod: "GET",
multiValueHeaders: {},
queryStringParameters: {},
isBase64Encoded: false,
body: null,
}
const res = await failingApp(myEvent)
expect(res).toEqual({
body: "",
isBase64Encoded: false,
multiValueHeaders: {},
statusCode: 500,
})
})
})