|
| 1 | +const should = require('should') |
| 2 | +const fastify = require('fastify') |
| 3 | +const fs = require('fs') |
| 4 | +const awsLambdaFastify = require('../index') |
| 5 | + |
| 6 | +describe('basic', () => { |
| 7 | + describe('GET', () => { |
| 8 | + it('should work as expected', async () => { |
| 9 | + const app = fastify() |
| 10 | + app.get('/test', async (request, reply) => { |
| 11 | + should(request.headers).have.property('x-my-header', 'wuuusaaa') |
| 12 | + should(request.headers).have.property('x-apigateway-event', '%7B%22httpMethod%22%3A%22GET%22%2C%22path%22%3A%22%2Ftest%22%2C%22headers%22%3A%7B%22X-My-Header%22%3A%22wuuusaaa%22%7D%7D') |
| 13 | + should(request.headers).have.property('user-agent', 'lightMyRequest') |
| 14 | + should(request.headers).have.property('host', 'localhost:80') |
| 15 | + should(request.headers).have.property('content-length', '0') |
| 16 | + reply.header('Set-Cookie', 'qwerty=one') |
| 17 | + reply.header('Set-Cookie', 'qwerty=two') |
| 18 | + reply.send({ hello: 'world' }) |
| 19 | + }) |
| 20 | + const proxy = awsLambdaFastify(app) |
| 21 | + const ret = await proxy({ |
| 22 | + httpMethod: 'GET', |
| 23 | + path: '/test', |
| 24 | + headers: { |
| 25 | + 'X-My-Header': 'wuuusaaa' |
| 26 | + } |
| 27 | + }) |
| 28 | + should(ret).have.property('statusCode', 200) |
| 29 | + should(ret).have.property('body', '{"hello":"world"}') |
| 30 | + should(ret).have.property('isBase64Encoded', false) |
| 31 | + should(ret).have.property('headers') |
| 32 | + should(ret.headers).have.property('content-type', 'application/json; charset=utf-8') |
| 33 | + should(ret.headers).have.property('content-length', '17') |
| 34 | + should(ret.headers).have.property('date') |
| 35 | + should(ret.headers).have.property('connection', 'keep-alive') |
| 36 | + should(ret.headers).have.property('Set-cookie', 'qwerty=one') |
| 37 | + should(ret.headers).have.property('sEt-cookie', 'qwerty=two') |
| 38 | + }) |
| 39 | + }) |
| 40 | + |
| 41 | + describe('GET with base64 encoding response', () => { |
| 42 | + let fileBuffer |
| 43 | + before((done) => { |
| 44 | + fs.readFile(__filename, (err, fb) => { |
| 45 | + fileBuffer = fb |
| 46 | + done(err) |
| 47 | + }) |
| 48 | + }) |
| 49 | + it('should work as expected', async () => { |
| 50 | + const app = fastify() |
| 51 | + app.get('/test', async (request, reply) => { |
| 52 | + should(request.headers).have.property('x-my-header', 'wuuusaaa') |
| 53 | + should(request.headers).have.property('x-apigateway-event', '%7B%22httpMethod%22%3A%22GET%22%2C%22path%22%3A%22%2Ftest%22%2C%22headers%22%3A%7B%22X-My-Header%22%3A%22wuuusaaa%22%2C%22Content-Type%22%3A%22application%2Fjson%22%7D%7D') |
| 54 | + should(request.headers).have.property('user-agent', 'lightMyRequest') |
| 55 | + should(request.headers).have.property('host', 'localhost:80') |
| 56 | + should(request.headers).have.property('content-length', '0') |
| 57 | + reply.header('Set-Cookie', 'qwerty=one') |
| 58 | + reply.header('Set-Cookie', 'qwerty=two') |
| 59 | + reply.send(fileBuffer) |
| 60 | + }) |
| 61 | + const proxy = awsLambdaFastify(app, { binaryMimeTypes: ['application/octet-stream'] }) |
| 62 | + const ret = await proxy({ |
| 63 | + httpMethod: 'GET', |
| 64 | + path: '/test', |
| 65 | + headers: { |
| 66 | + 'X-My-Header': 'wuuusaaa', |
| 67 | + 'Content-Type': 'application/json' |
| 68 | + } |
| 69 | + }) |
| 70 | + should(ret).have.property('statusCode', 200) |
| 71 | + should(ret).have.property('body', fileBuffer.toString('base64')) |
| 72 | + should(ret).have.property('isBase64Encoded', true) |
| 73 | + should(ret).have.property('headers') |
| 74 | + should(ret.headers).have.property('content-type', 'application/octet-stream') |
| 75 | + should(ret.headers).have.property('content-length') |
| 76 | + should(ret.headers).have.property('date') |
| 77 | + should(ret.headers).have.property('connection', 'keep-alive') |
| 78 | + should(ret.headers).have.property('Set-cookie', 'qwerty=one') |
| 79 | + should(ret.headers).have.property('sEt-cookie', 'qwerty=two') |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + describe('POST', () => { |
| 84 | + it('should work as expected', async () => { |
| 85 | + const app = fastify() |
| 86 | + app.post('/test', async (request, reply) => { |
| 87 | + should(request.headers).have.property('content-type', 'application/json') |
| 88 | + should(request.headers).have.property('x-my-header', 'wuuusaaa') |
| 89 | + should(request.headers).have.property('x-apigateway-event', '%7B%22httpMethod%22%3A%22POST%22%2C%22path%22%3A%22%2Ftest%22%2C%22headers%22%3A%7B%22X-My-Header%22%3A%22wuuusaaa%22%2C%22Content-Type%22%3A%22application%2Fjson%22%7D%7D') |
| 90 | + should(request.headers).have.property('user-agent', 'lightMyRequest') |
| 91 | + should(request.headers).have.property('host', 'localhost:80') |
| 92 | + should(request.headers).have.property('content-length', 14) |
| 93 | + should(request.body).have.property('greet', 'hi') |
| 94 | + reply.header('Set-Cookie', 'qwerty=one') |
| 95 | + reply.header('Set-Cookie', 'qwerty=two') |
| 96 | + reply.send({ hello: 'world2' }) |
| 97 | + }) |
| 98 | + const proxy = awsLambdaFastify(app) |
| 99 | + const ret = await proxy({ |
| 100 | + httpMethod: 'POST', |
| 101 | + path: '/test', |
| 102 | + headers: { |
| 103 | + 'X-My-Header': 'wuuusaaa', |
| 104 | + 'Content-Type': 'application/json' |
| 105 | + }, |
| 106 | + body: '{"greet":"hi"}' |
| 107 | + }) |
| 108 | + should(ret).have.property('statusCode', 200) |
| 109 | + should(ret).have.property('body', '{"hello":"world2"}') |
| 110 | + should(ret).have.property('isBase64Encoded', false) |
| 111 | + should(ret).have.property('headers') |
| 112 | + should(ret.headers).have.property('content-type', 'application/json; charset=utf-8') |
| 113 | + should(ret.headers).have.property('content-length', '18') |
| 114 | + should(ret.headers).have.property('date') |
| 115 | + should(ret.headers).have.property('connection', 'keep-alive') |
| 116 | + should(ret.headers).have.property('Set-cookie', 'qwerty=one') |
| 117 | + should(ret.headers).have.property('sEt-cookie', 'qwerty=two') |
| 118 | + }) |
| 119 | + }) |
| 120 | + |
| 121 | + describe('POST with base64 encoding', () => { |
| 122 | + it('should work as expected', async () => { |
| 123 | + const app = fastify() |
| 124 | + app.post('/test', async (request, reply) => { |
| 125 | + should(request.headers).have.property('content-type', 'application/json') |
| 126 | + should(request.headers).have.property('x-my-header', 'wuuusaaa') |
| 127 | + should(request.headers).have.property('x-apigateway-event', '%7B%22httpMethod%22%3A%22POST%22%2C%22path%22%3A%22%2Ftest%22%2C%22headers%22%3A%7B%22X-My-Header%22%3A%22wuuusaaa%22%2C%22Content-Type%22%3A%22application%2Fjson%22%7D%2C%22isBase64Encoded%22%3Atrue%7D') |
| 128 | + should(request.headers).have.property('user-agent', 'lightMyRequest') |
| 129 | + should(request.headers).have.property('host', 'localhost:80') |
| 130 | + should(request.headers).have.property('content-length', 15) |
| 131 | + should(request.body).have.property('greet', 'hi') |
| 132 | + reply.header('Set-Cookie', 'qwerty=one') |
| 133 | + reply.header('Set-Cookie', 'qwerty=two') |
| 134 | + reply.send({ hello: 'world2' }) |
| 135 | + }) |
| 136 | + const proxy = awsLambdaFastify(app) |
| 137 | + const ret = await proxy({ |
| 138 | + httpMethod: 'POST', |
| 139 | + path: '/test', |
| 140 | + headers: { |
| 141 | + 'X-My-Header': 'wuuusaaa', |
| 142 | + 'Content-Type': 'application/json' |
| 143 | + }, |
| 144 | + body: 'eyJncmVldCI6ICJoaSJ9', |
| 145 | + isBase64Encoded: true |
| 146 | + }) |
| 147 | + should(ret).have.property('statusCode', 200) |
| 148 | + should(ret).have.property('body', '{"hello":"world2"}') |
| 149 | + should(ret).have.property('isBase64Encoded', false) |
| 150 | + should(ret).have.property('headers') |
| 151 | + should(ret.headers).have.property('content-type', 'application/json; charset=utf-8') |
| 152 | + should(ret.headers).have.property('content-length', '18') |
| 153 | + should(ret.headers).have.property('date') |
| 154 | + should(ret.headers).have.property('connection', 'keep-alive') |
| 155 | + should(ret.headers).have.property('Set-cookie', 'qwerty=one') |
| 156 | + should(ret.headers).have.property('sEt-cookie', 'qwerty=two') |
| 157 | + }) |
| 158 | + }) |
| 159 | +}) |
0 commit comments