forked from jeremydaly/lambda-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequests.js
More file actions
198 lines (179 loc) · 10.8 KB
/
Copy pathrequests.js
File metadata and controls
198 lines (179 loc) · 10.8 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
'use strict';
const expect = require('chai').expect // Assertion library
// Init API instance
const api = require('../index')({ version: 'v1.0' })
/******************************************************************************/
/*** DEFINE TEST ROUTES ***/
/******************************************************************************/
api.get('/test/hello', function(req,res) {
let request = Object.assign(req,{app:null})
// console.log(JSON.stringify(request,null,2));
res.cookie('test','value')
res.cookie('test2','value2')
res.status(200).json({ request })
})
api.get('/test/201', function(req,res) {
let request = Object.assign(req,{app:null})
res.status(201).json({ request })
})
/******************************************************************************/
/*** BEGIN TESTS ***/
/******************************************************************************/
describe('Request Tests:', function() {
describe('API Gateway Proxy Event', function() {
it('Standard event', async function() {
let _event = require('./sample-event-apigateway1.json')
let _context = require('./sample-context-apigateway1.json')
let result = await new Promise(r => api.run(_event,_context,(e,res) => { r(res) }))
let body = JSON.parse(result.body)
// console.log(body);
// console.log(body.request.multiValueHeaders);
expect(result.multiValueHeaders).to.deep.equal({ 'content-type': ['application/json'], 'set-cookie': ['test=value; Path=/','test2=value2; Path=/'] })
expect(body).to.have.property('request')
expect(body.request.id).is.not.null
expect(body.request.interface).to.equal('apigateway')
expect(body.request.userAgent).to.equal('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48')
expect(body.request).to.have.property('requestContext')
expect(body.request.ip).to.equal('192.168.100.1')
expect(body.request.pathParameters).to.deep.equal({ "proxy": "hello" })
expect(body.request.stageVariables).to.deep.equal({ "stageVarName": "stageVarValue" })
expect(body.request.isBase64Encoded).to.equal(false)
expect(body.request.clientType).to.equal('desktop')
expect(body.request.clientCountry).to.equal('US')
expect(body.request.route).to.equal('/test/hello')
expect(body.request.query.qs1).to.equal('foo')
expect(body.request.query.qs2).to.equal('bar')
expect(body.request.multiValueQuery.qs2).to.deep.equal(['foo','bar'])
expect(body.request.multiValueQuery.qs3).to.deep.equal(['bat','baz'])
expect(body.request.headers['test-header']).to.equal('val1,val2')
expect(body.request.multiValueHeaders['test-header']).to.deep.equal(['val1','val2'])
})
it('Missing X-Forwarded-For (sourceIp fallback)', async function() {
let _event = require('./sample-event-apigateway1.json')
let _context = require('./sample-context-apigateway1.json')
delete _event.headers['X-Forwarded-For'] // remove the header
delete _event.multiValueHeaders['x-forwarded-for'] // remove the header
let result = await new Promise(r => api.run(_event,_context,(e,res) => { r(res) }))
let body = JSON.parse(result.body)
expect(result.multiValueHeaders).to.deep.equal({ 'content-type': ['application/json'], 'set-cookie': ['test=value; Path=/','test2=value2; Path=/'] })
expect(body).to.have.property('request')
expect(body.request.id).is.not.null
expect(body.request.interface).to.equal('apigateway')
expect(body.request.userAgent).to.equal('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48')
expect(body.request).to.have.property('requestContext')
expect(body.request.ip).to.equal('192.168.100.12')
expect(body.request.pathParameters).to.deep.equal({ "proxy": "hello" })
expect(body.request.stageVariables).to.deep.equal({ "stageVarName": "stageVarValue" })
expect(body.request.isBase64Encoded).to.equal(false)
expect(body.request.clientType).to.equal('desktop')
expect(body.request.clientCountry).to.equal('US')
expect(body.request.route).to.equal('/test/hello')
expect(body.request.query.qs1).to.equal('foo')
expect(body.request.query.qs2).to.equal('bar')
expect(body.request.multiValueQuery.qs2).to.deep.equal(['foo','bar'])
expect(body.request.multiValueQuery.qs3).to.deep.equal(['bat','baz'])
expect(body.request.headers['test-header']).to.equal('val1,val2')
expect(body.request.multiValueHeaders['test-header']).to.deep.equal(['val1','val2'])
// console.log(body);
})
})
describe('ALB Event', function() {
it('Standard event', async function() {
let _event = require('./sample-event-alb1.json')
let _context = require('./sample-context-alb1.json')
let result = await new Promise(r => api.run(_event,_context,(e,res) => { r(res) }))
let body = JSON.parse(result.body)
// console.log(JSON.stringify(result,null,2));
expect(result.headers).to.deep.equal({ 'content-type': 'application/json', 'set-cookie': 'test2=value2; Path=/' })
expect(body).to.have.property('request')
expect(result.statusDescription).to.equal('200 OK')
expect(body.request.id).is.not.null
expect(body.request.interface).to.equal('alb')
expect(body.request.userAgent).to.equal('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48')
expect(body.request).to.have.property('requestContext')
expect(body.request.ip).to.equal('192.168.100.1')
expect(body.request.isBase64Encoded).to.equal(true)
expect(body.request.clientType).to.equal('unknown')
expect(body.request.clientCountry).to.equal('unknown')
expect(body.request.route).to.equal('/test/hello')
expect(body.request.query.qs1).to.equal('foo')
expect(body.request.multiValueQuery.qs1).to.deep.equal(['foo'])
})
it('With multi-value support', async function() {
let _event = require('./sample-event-alb2.json')
let _context = require('./sample-context-alb1.json')
let result = await new Promise(r => api.run(_event,_context,(e,res) => { r(res) }))
let body = JSON.parse(result.body)
// console.log(JSON.stringify(result,null,2));
expect(result.multiValueHeaders).to.deep.equal({ 'content-type': ['application/json'], 'set-cookie': ['test=value; Path=/','test2=value2; Path=/'] })
expect(body).to.have.property('request')
expect(result.statusDescription).to.equal('200 OK')
expect(body.request.id).is.not.null
expect(body.request.interface).to.equal('alb')
expect(body.request.userAgent).to.equal('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48')
expect(body.request).to.have.property('requestContext')
expect(body.request.ip).to.equal('192.168.100.1')
expect(body.request.isBase64Encoded).to.equal(true)
expect(body.request.clientType).to.equal('unknown')
expect(body.request.clientCountry).to.equal('unknown')
expect(body.request.route).to.equal('/test/hello')
expect(body.request.query.qs1).to.equal('foo')
expect(body.request.multiValueQuery.qs1).to.deep.equal(['foo'])
expect(body.request.multiValueQuery.qs2).to.deep.equal(['foo','bar'])
expect(body.request.multiValueQuery.qs3).to.deep.equal(['foo','bar','bat'])
expect(body.request.headers['test-header']).to.equal('val1,val2')
expect(body.request.multiValueHeaders['test-header']).to.deep.equal(['val1','val2'])
})
it('Alternate status code', async function() {
let _event = Object.assign(require('./sample-event-alb2.json'),{ path: '/test/201' })
let _context = require('./sample-context-alb1.json')
let result = await new Promise(r => api.run(_event,_context,(e,res) => { r(res) }))
let body = JSON.parse(result.body)
// console.log(JSON.stringify(result,null,2));
expect(result.multiValueHeaders).to.deep.equal({ 'content-type': ['application/json'] })
expect(result.statusDescription).to.equal('201 Created')
expect(body).to.have.property('request')
expect(body.request.id).is.not.null
expect(body.request.interface).to.equal('alb')
expect(body.request.userAgent).to.equal('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48')
expect(body.request).to.have.property('requestContext')
expect(body.request.ip).to.equal('192.168.100.1')
expect(body.request.isBase64Encoded).to.equal(true)
expect(body.request.clientType).to.equal('unknown')
expect(body.request.clientCountry).to.equal('unknown')
expect(body.request.route).to.equal('/test/201')
expect(body.request.query.qs1).to.equal('foo')
expect(body.request.multiValueQuery.qs1).to.deep.equal(['foo'])
expect(body.request.multiValueQuery.qs2).to.deep.equal(['foo','bar'])
expect(body.request.multiValueQuery.qs3).to.deep.equal(['foo','bar','bat'])
expect(body.request.headers['test-header']).to.equal('val1,val2')
expect(body.request.multiValueHeaders['test-header']).to.deep.equal(['val1','val2'])
})
})
describe('API Gateway Console Test', function() {
// See: https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-test-method.html
it('Standard event w/o multiValueHeaders', async function() {
let _event = require('./sample-event-consoletest1.json')
let _context = require('./sample-context-apigateway1.json')
let result = await new Promise(r => api.run(_event,_context,(e,res) => { r(res) }))
let body = JSON.parse(result.body)
// console.log(body);
// console.log(body.request.multiValueHeaders);
expect(body).to.have.property('request')
expect(body.request.id).is.not.null
expect(body.request.interface).to.equal('apigateway')
expect(body.request).to.have.property('requestContext')
expect(body.request.ip).to.equal('test-invoke-source-ip')
expect(body.request.pathParameters).to.deep.equal({ "proxy": "test/hello" })
expect(body.request.stageVariables).to.deep.equal({})
expect(body.request.isBase64Encoded).to.equal(false)
expect(body.request.clientType).to.equal('unknown')
expect(body.request.clientCountry).to.equal('unknown')
expect(body.request.route).to.equal('/test/hello')
expect(body.request.query).to.deep.equal({})
expect(body.request.multiValueQuery).to.deep.equal({})
expect(body.request.headers).to.deep.equal({})
// NOTE: body.request.multiValueHeaders is null in this case
})
})
}) // end Request tests