├── .gitignore ├── README.md ├── binary └── index.js ├── data └── sample.pdf ├── package.json ├── serverless.yml ├── test └── binary.spec.js └── text └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | .idea 4 | .serverless 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Binary files in API Gateway/Lambda test 2 | 3 | Note: For this to work in a browser you have to add "\*/\*" to binary Media Types in API Gateway console. 4 | 5 | If you don't need to support browsers you can instead add "application/pdf" there, and request with Accept header like so: 6 | 7 | curl -H "Accept: application/pdf" https://xxxxxxxx.execute-api.eu-west-1.amazonaws.com/dev/fetchBinary > test.pdf 8 | -------------------------------------------------------------------------------- /binary/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const fs = require("fs"); 4 | 5 | module.exports.fetchBinary = (event, context, callback) => { 6 | const content = fs.readFileSync("data/sample.pdf"); 7 | 8 | const response = { 9 | statusCode: 200, 10 | headers: { 11 | "Content-Type": "application/pdf", 12 | "Content-Disposition": "inline; filename=\"sample.pdf\"" 13 | }, 14 | body: content.toString("base64"), 15 | isBase64Encoded: true 16 | }; 17 | 18 | return callback(null, response); 19 | }; 20 | -------------------------------------------------------------------------------- /data/sample.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bni/binary-test/1d79c78a3e2dc57694a64eefea459a8f5c1035d5/data/sample.pdf -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "binary-test", 3 | "version": "0.0.1", 4 | "description": "Binary Test", 5 | "author": "bnilsson@me.com", 6 | "license": "MIT", 7 | "repository": { 8 | "type": "git", 9 | "url": "https://github.com/bni/binary-test" 10 | }, 11 | "scripts": { 12 | "test": "./node_modules/.bin/mocha" 13 | }, 14 | "dependencies": { 15 | "moment": "2.18.1", 16 | "aws-sdk": "2.32.0", 17 | "request": "2.81.0", 18 | "soap": "0.19.0", 19 | "mock-require": "2.0.1" 20 | }, 21 | "devDependencies": { 22 | "chai": "3.5.0", 23 | "mocha": "3.2.0", 24 | "serverless": "1.9.0", 25 | "serverless-plugin-browserify": "1.1.2" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | service: binary-test 2 | 3 | package: 4 | individually: true 5 | 6 | plugins: 7 | - serverless-plugin-browserify 8 | 9 | provider: 10 | name: aws 11 | runtime: nodejs6.10 12 | profile: default 13 | region: eu-west-1 14 | stage: dev 15 | 16 | functions: 17 | fetchBinary: 18 | handler: binary/index.fetchBinary 19 | package: 20 | include: 21 | - data/sample.pdf 22 | memorySize: 1024 23 | timeout: 10 24 | events: 25 | - http: 26 | path: fetchBinary 27 | method: GET 28 | 29 | fetchText: 30 | handler: text/index.fetchText 31 | memorySize: 1024 32 | timeout: 10 33 | events: 34 | - http: 35 | path: fetchText 36 | method: GET 37 | -------------------------------------------------------------------------------- /test/binary.spec.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const assert = require("chai").assert; 4 | 5 | describe("binary", () => { 6 | it("Should return one response", (done) => { 7 | try { 8 | const index = require("../binary/index"); 9 | 10 | index.fetchBinary({}, {}, (error, response) => { 11 | assert.isString(response.body); 12 | 13 | const decodedContent = new Buffer(response.body, "base64"); 14 | 15 | assert.deepEqual(decodedContent.slice(0, 4), new Buffer([0x25, 0x50, 0x44, 0x46]), "Invalid PDF"); 16 | 17 | done(); 18 | }); 19 | } catch (errorMessage) { 20 | done(errorMessage); 21 | } 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /text/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const moment = require("moment"); 4 | const S3 = require("aws-sdk/clients/s3"); // Not used, for now 5 | const request = require("request"); // Not used, for now 6 | 7 | // Hack around broken soap module that tries to read .ejs files that dont exist 8 | const mock = require("mock-require"); mock("fs", {readFileSync: () => {return "";}}); 9 | const soap = require("soap"); mock.stop("fs"); 10 | 11 | module.exports.fetchText = (event, context, callback) => { 12 | const response = { 13 | statusCode: 200, 14 | headers: { 15 | "Content-Type": "text/plain" 16 | }, 17 | body: `Today is ${moment().format("dddd")}.` 18 | }; 19 | 20 | return callback(null, response); 21 | }; 22 | --------------------------------------------------------------------------------