-
Notifications
You must be signed in to change notification settings - Fork 1
/
aws-lambda-http.int-spec.ts
106 lines (85 loc) 路 2.96 KB
/
aws-lambda-http.int-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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { AwsLambdaHttpModule, LambdaHandler, LambdaHandlerFn, Lambda } from '@dandi-contrib/aws-lambda'
import { APIGatewayProxyEvent } from '@dandi-contrib/aws-lambda/node_modules/@types/aws-lambda'
import { Injectable } from '@dandi/core'
import { HttpHeader, HttpMethod, HttpModule, HttpStatusCode, MimeType } from '@dandi/http'
import { QueryParam, RequestModel } from '@dandi/http-model'
import { HttpPipelineModule, NativeJsonObjectRenderer } from '@dandi/http-pipeline'
import { Property, Required } from '@dandi/model'
import { ModelBuilderModule } from '@dandi/model-builder'
import { expect } from 'chai'
describe('AWS Lambda Http Events', () => {
@Injectable()
class TestGetHandler implements LambdaHandler {
public handleEvent(@QueryParam(Number) foo: string): any {
return {
message: 'hello!',
foo,
}
}
}
class TestModel {
@Property(String)
@Required()
public message: string
}
@Injectable()
class TestPostHandler implements LambdaHandler {
public handleEvent(@RequestModel(TestModel) model: TestModel): any {
return {
message: model.message,
}
}
}
let handler: LambdaHandlerFn
afterEach(() => {
handler.dispose('test complete')
handler = undefined
})
describe('GET requests', () => {
beforeEach(() => {
handler = Lambda.handler(TestGetHandler, HttpModule, HttpPipelineModule.defaultRenderer(NativeJsonObjectRenderer), ModelBuilderModule, AwsLambdaHttpModule)
})
it('can handle a simple GET request', async () => {
const event: APIGatewayProxyEvent = {
httpMethod: HttpMethod.get,
headers: {},
queryStringParameters: {},
multiValueQueryStringParameters: {},
} as APIGatewayProxyEvent
const result = await handler(event, {} as any)
expect(result).to.deep.equal({
body: JSON.stringify({ message: 'hello!' }),
headers: {
[HttpHeader.contentType]: MimeType.applicationJson,
},
statusCode: HttpStatusCode.ok,
})
})
})
describe('POST requests', () => {
beforeEach(() => {
handler = Lambda.handler(TestPostHandler, HttpModule, HttpPipelineModule.defaultRenderer(NativeJsonObjectRenderer), ModelBuilderModule, AwsLambdaHttpModule)
})
it('can handle a simple POST request', async () => {
const event: APIGatewayProxyEvent = {
httpMethod: HttpMethod.post,
headers: {
[HttpHeader.contentType]: 'application/json',
},
body: JSON.stringify({
message: 'hi!',
}),
queryStringParameters: {},
multiValueQueryStringParameters: {},
} as unknown as APIGatewayProxyEvent
const result = await handler(event, {} as any)
expect(result).to.deep.equal({
body: JSON.stringify({ message: 'hi!' }),
headers: {
[HttpHeader.contentType]: MimeType.applicationJson,
},
statusCode: HttpStatusCode.created,
})
})
})
})