Skip to content

Commit

Permalink
feat(binary): allow wildcards and extensions in binary types (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
forabi authored and brettstack committed Apr 10, 2018
1 parent eaf8735 commit 8879cf3
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
38 changes: 38 additions & 0 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,42 @@ describe('forwardResponseToApiGateway: content-type encoding', () => {
isBase64Encoded: false
}))
})

test('wildcards in binary types array', () => {
const server = new MockServer(['image/*'])
const headers = {'content-type': 'image/jpeg'}
const body = 'hello world'
const response = new MockResponse(200, headers, body)
return new Promise(
(resolve, reject) => {
const context = new MockContext(resolve)
awsServerlessExpress.forwardResponseToApiGateway(
server, response, context)
}
).then(successResponse => expect(successResponse).toEqual({
statusCode: 200,
body: new Buffer(body).toString('base64'),
headers: headers,
isBase64Encoded: true
}))
})

test('extensions in binary types array', () => {
const server = new MockServer(['.png'])
const headers = {'content-type': 'image/png'}
const body = 'hello world'
const response = new MockResponse(200, headers, body)
return new Promise(
(resolve, reject) => {
const context = new MockContext(resolve)
awsServerlessExpress.forwardResponseToApiGateway(
server, response, context)
}
).then(successResponse => expect(successResponse).toEqual({
statusCode: 200,
body: new Buffer(body).toString('base64'),
headers: headers,
isBase64Encoded: true
}))
})
})
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
const http = require('http')
const url = require('url')
const binarycase = require('binary-case')
const isType = require('type-is')

function getPathWithQueryStringParams(event) {
return url.format({ pathname: event.path, query: event.queryStringParameters })
Expand All @@ -27,7 +28,7 @@ function getContentType(params) {
}

function isContentTypeBinaryMimeType(params) {
return params.binaryMimeTypes.indexOf(params.contentType) !== -1
return params.binaryMimeTypes.length > 0 && !!isType.is(params.contentType, params.binaryMimeTypes)
}

function mapApiGatewayEventToHttpRequest(event, context, socketPath) {
Expand Down
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
"security-scan": "nsp check"
},
"dependencies": {
"binary-case": "^1.0.0"
"binary-case": "^1.0.0",
"type-is": "^1.6.16"
},
"config": {
"commitizen": {
Expand Down

0 comments on commit 8879cf3

Please sign in to comment.