forked from jeremydaly/lambda-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
350 lines (261 loc) · 11.6 KB
/
Copy pathutils.js
File metadata and controls
350 lines (261 loc) · 11.6 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
'use strict';
const expect = require('chai').expect // Assertion library
const utils = require('../lib/utils')
/******************************************************************************/
/*** BEGIN TESTS ***/
/******************************************************************************/
describe('Utility Function Tests:', function() {
describe('escapeHtml:', function() {
it('Escape &, <, >, ", and \'', function() {
expect(utils.escapeHtml('&<>"\'')).to.equal('&<>"'')
}) // end it
}) // end escapeHtml tests
describe('encodeUrl:', function() {
it('Unencoded with space in param', function() {
expect(utils.encodeUrl('http://www.github.com/?foo=bar with space')).to.equal('http://www.github.com/?foo=bar%20with%20space')
}) // end it
it('Encoded URL with additional invalid sequence', function() {
expect(utils.encodeUrl('http://www.github.com/?foo=bar%20with%20space%foo')).to.equal('http://www.github.com/?foo=bar%20with%20space%25foo')
}) // end it
it('Encode special characters, double encode, decode', function() {
let url = 'http://www.github.com/?foo=шеллы'
let encoded = utils.encodeUrl(url)
let doubleEncoded = utils.encodeUrl(encoded)
let decoded = decodeURI(encoded)
expect(encoded).to.equal('http://www.github.com/?foo=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B')
expect(doubleEncoded).to.equal('http://www.github.com/?foo=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B')
expect(decoded).to.equal(url)
}) // end it
}) // end encodeUrl tests
describe('encodeBody:', function() {
it('String', function() {
expect(utils.encodeBody('test string')).to.equal('test string')
}) // end it
it('Number', function() {
expect(utils.encodeBody(123)).to.equal('123')
}) // end it
it('Array', function() {
expect(utils.encodeBody([1,2,3])).to.equal('[1,2,3]')
}) // end it
it('Object', function() {
expect(utils.encodeBody({ foo: 'bar' })).to.equal('{"foo":"bar"}')
}) // end it
}) // end encodeBody tests
describe('parseBody:', function() {
it('String', function() {
expect(utils.parseBody('test string')).to.equal('test string')
}) // end it
it('Number', function() {
expect(utils.parseBody('123')).to.equal(123)
}) // end it
it('Array', function() {
expect(utils.parseBody('[1,2,3]')).to.deep.equal([ 1, 2, 3 ])
}) // end it
it('Object', function() {
expect(utils.parseBody('{"foo":"bar"}')).to.deep.equal({ foo: 'bar' })
}) // end it
}) // end encodeBody tests
describe('parseAuth:', function() {
it('None: undefined', function() {
let result = utils.parseAuth(undefined)
expect(result).to.deep.equal({ type: 'none', value: null })
}) // end it
it('None: empty', function() {
let result = utils.parseAuth('')
expect(result).to.deep.equal({ type: 'none', value: null })
}) // end it
it('Invalid schema', function() {
let result = utils.parseAuth('Test 12345')
expect(result).to.deep.equal({ type: 'none', value: null })
}) // end it
it('Missing value/token', function() {
let result = utils.parseAuth('Bearer')
expect(result).to.deep.equal({ type: 'none', value: null })
}) // end it
it('Bearer Token (OAuth2/JWT)', function() {
let result = utils.parseAuth('Bearer XYZ')
expect(result).to.deep.equal({ type: 'Bearer', value: 'XYZ' })
}) // end it
it('Digest', function() {
let result = utils.parseAuth('Digest XYZ')
expect(result).to.deep.equal({ type: 'Digest', value: 'XYZ' })
}) // end it
it('OAuth 1.0', function() {
let result = utils.parseAuth('OAuth realm="Example", oauth_consumer_key="xyz", oauth_token="abc", oauth_version="1.0"')
expect(result).to.deep.equal({
type: 'OAuth',
value: 'realm="Example", oauth_consumer_key="xyz", oauth_token="abc", oauth_version="1.0"',
realm: 'Example',
oauth_consumer_key: 'xyz',
oauth_token: 'abc',
oauth_version: '1.0'
})
}) // end it
it('Basic', function() {
let creds = new Buffer('test:testing').toString('base64')
let result = utils.parseAuth('Basic ' + creds)
expect(result).to.deep.equal({ type: 'Basic', value: creds, username: 'test', password: 'testing' })
}) // end it
it('Basic (no password)', function() {
let creds = new Buffer('test').toString('base64')
let result = utils.parseAuth('Basic ' + creds)
expect(result).to.deep.equal({ type: 'Basic', value: creds, username: 'test', password: null })
}) // end it
it('Invalid type', function() {
let result = utils.parseAuth(123)
expect(result).to.deep.equal({ type: 'none', value: null })
}) // end it
}) // end encodeBody tests
describe('mimeLookup:', function() {
it('.pdf', function() {
expect(utils.mimeLookup('.pdf')).to.equal('application/pdf')
}) // end it
it('application/pdf', function() {
expect(utils.mimeLookup('application/pdf')).to.equal('application/pdf')
}) // end it
it('application-x/pdf (non-standard w/ slash)', function() {
expect(utils.mimeLookup('application-x/pdf')).to.equal('application-x/pdf')
}) // end it
it('xml', function() {
expect(utils.mimeLookup('xml')).to.equal('application/xml')
}) // end it
it('.html', function() {
expect(utils.mimeLookup('.html')).to.equal('text/html')
}) // end it
it('css', function() {
expect(utils.mimeLookup('css')).to.equal('text/css')
}) // end it
it('jpg', function() {
expect(utils.mimeLookup('jpg')).to.equal('image/jpeg')
}) // end it
it('.svg', function() {
expect(utils.mimeLookup('.svg')).to.equal('image/svg+xml')
}) // end it
it('docx', function() {
expect(utils.mimeLookup('docx')).to.equal('application/vnd.openxmlformats-officedocument.wordprocessingml.document')
}) // end it
it('Custom', function() {
expect(utils.mimeLookup('.mpeg', { mpeg: 'video/mpeg' })).to.equal('video/mpeg')
}) // end it
}) // end encodeBody tests
describe('extractRoutes:', function() {
it('Sample routes', function() {
// Create an api instance
let api = require('../index')()
api.get('/', (req,res) => {})
api.post('/test', (req,res) => {})
api.put('/test/put', (req,res) => {})
api.delete('/test/:var/delete', (req,res) => {})
expect(utils.extractRoutes(api._routes)).to.deep.equal([
[ 'GET', '/' ],
[ 'POST', '/test' ],
[ 'PUT', '/test/put' ],
[ 'DELETE', '/test/:var/delete' ]
])
}) // end it
it('No routes', function() {
// Create an api instance
let api = require('../index')()
expect(utils.extractRoutes(api._routes)).to.deep.equal([])
}) // end it
it('Prefixed routes', function() {
// Create an api instance
let api = require('../index')()
api.register((apix,opts) => {
apix.get('/', (req,res) => {})
apix.post('/test', (req,res) => {})
}, { prefix: '/v1' })
api.get('/', (req,res) => {})
api.post('/test', (req,res) => {})
api.put('/test/put', (req,res) => {})
api.delete('/test/:var/delete', (req,res) => {})
expect(utils.extractRoutes(api._routes)).to.deep.equal([
[ 'GET', '/v1' ],
[ 'POST', '/v1/test' ],
[ 'GET', '/' ],
[ 'POST', '/test' ],
[ 'PUT', '/test/put' ],
[ 'DELETE', '/test/:var/delete' ]
])
}) // end it
it('Base routes', function() {
// Create an api instance
let api = require('../index')({ base: 'v2' })
api.get('/', (req,res) => {})
api.post('/test', (req,res) => {})
api.put('/test/put', (req,res) => {})
api.delete('/test/:var/delete', (req,res) => {})
expect(utils.extractRoutes(api._routes)).to.deep.equal([
[ 'GET', '/v2' ],
[ 'POST', '/v2/test' ],
[ 'PUT', '/v2/test/put' ],
[ 'DELETE', '/v2/test/:var/delete' ]
])
}) // end it
}) // end extractRoutes
describe('generateEtag:', function() {
it('Sample text', function() {
expect(utils.generateEtag('this is a test string')).to.equal('f6774519d1c7a3389ef327e9c04766b9')
}) // end it
it('Sample object', function() {
expect(utils.generateEtag({ test: true, foo: 'bar' })).to.equal('def7648849c1e7f30c9a9c0ac79e4e52')
}) // end it
it('Sample JSON string object', function() {
expect(utils.generateEtag(JSON.stringify({ test: true, foo: 'bar' }))).to.equal('def7648849c1e7f30c9a9c0ac79e4e52')
}) // end it
it('Sample buffer', function() {
expect(utils.generateEtag(Buffer.from('this is a test string as a buffer'))).to.equal('6a2f7473a72cfebc96ae8cf93d643b70')
}) // end it
it('Long string', function() {
let longString = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
expect(utils.generateEtag(longString)).to.equal('2d8c2f6d978ca21712b5f6de36c9d31f')
}) // end it
it('Long string (minor variant)', function() {
let longString = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est Laborum.'
expect(utils.generateEtag(longString)).to.equal('bc82a4065a8ab48ade900c6466b19ccd')
}) // end it
}) // end generateEtag tests
describe('isS3:', function() {
it('Empty path', function() {
expect(utils.isS3('')).to.be.false
})
it('Valid S3 path', function() {
expect(utils.isS3('s3://test-bucket/key')).to.be.true
})
it('Valid S3 path (uppercase)', function() {
expect(utils.isS3('S3://test-bucket/key')).to.be.true
})
it('Invalid S3 path', function() {
expect(utils.isS3('s3://test-bucket')).to.be.false
})
it('Empty S3 path', function() {
expect(utils.isS3('s3:///')).to.be.false
})
it('URL', function() {
expect(utils.isS3('https://somedomain.com/test')).to.be.false
})
it('Relative path', function() {
expect(utils.isS3('../test/file.txt')).to.be.false
})
}) // end isS3 tests
describe('parseS3:', function() {
it('Valid S3 path', function() {
expect(utils.parseS3('s3://test-bucket/key')).to.deep.equal({ Bucket: 'test-bucket', Key: 'key' })
})
it('Valid S3 path (nested key)', function() {
expect(utils.parseS3('s3://test-bucket/key/path/file.txt')).to.deep.equal({ Bucket: 'test-bucket', Key: 'key/path/file.txt' })
})
it('Invalid S3 path (no key)', function() {
let func = () => utils.parseS3('s3://test-bucket')
expect(func).to.throw('Invalid S3 path')
})
it('Invalid S3 path (no bucket or key)', function() {
let func = () => utils.parseS3('s3://')
expect(func).to.throw('Invalid S3 path')
})
it('Invalid S3 path (empty)', function() {
let func = () => utils.parseS3('')
expect(func).to.throw('Invalid S3 path')
})
}) // end parseS3 tests
}) // end UTILITY tests