Skip to content

Commit

Permalink
added some multipart tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Oct 5, 2022
1 parent a85d4e2 commit e274f7e
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"performance": "npm run lint && node performanceTest/test"
},
"devDependencies": {
"@fastify/multipart": "7.2.0",
"@types/aws-lambda": "8.10.106",
"aws-lambda": "^1.0.7",
"aws-serverless-express": "^3.4.0",
Expand Down
106 changes: 106 additions & 0 deletions test/multipart.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const { test } = require('tap')
const fastify = require('fastify')
const awsLambdaFastify = require('../index')
const multipart = require('@fastify/multipart')

test('should parse the multipart form-data successfully given raw multipart form data', async (t) => {
t.plan(8)

const app = fastify()
const evt = {
version: '2.0',
httpMethod: 'POST',
path: '/test',
headers: {
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryDP6Z1qHQSzB6Pf8c'
},
body: ['------WebKitFormBoundaryDP6Z1qHQSzB6Pf8c',
'Content-Disposition: form-data; name="uploadFile1"; filename="test.txt"',
'Content-Type: text/plain',
'',
'Hello World!',
'------WebKitFormBoundaryDP6Z1qHQSzB6Pf8c--'
].join('\r\n'),
isBase64Encoded: false
}

app.register(multipart, { attachFieldsToBody: true })
app.post('/test', async (request, reply) => {
t.equal(request.body.uploadFile1.fieldname, 'uploadFile1')
t.equal(request.body.uploadFile1.filename, 'test.txt')
t.equal(request.body.uploadFile1.encoding, '7bit')
t.equal(request.body.uploadFile1.mimetype, 'text/plain')
t.equal(Buffer.from(request.body.uploadFile1._buf).toString('utf-8'), 'Hello World!')
reply.send({ hello: 'world' })
})
const proxy = awsLambdaFastify(app, { serializeLambdaArguments: true })
const ret = await proxy(evt)
t.equal(ret.statusCode, 200)
t.equal(ret.body, '{"hello":"world"}')
t.equal(ret.isBase64Encoded, false)
})

test('should parse the multipart form-data successfully given base64 encoded form data', async (t) => {
t.plan(8)

const app = fastify()
const evt = {
version: '2.0',
httpMethod: 'POST',
path: '/test',
headers: {
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryDP6Z1qHQSzB6Pf8c'
},
body: `LS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5RFA2WjFxSFFTekI2UGY4Yw0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJ1cGxvYWRGaWxlMSI7IGZpb
GVuYW1lPSJ0ZXN0LnR4dCINCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbg0KDQpIZWxsbyBXb3JsZCENCi0tLS0tLVdlYktpdEZvcm1Cb3VuZGFyeURQNloxcUhRU3pCNl
BmOGMNCkNvbnRlbnQtRGlzcG9zaXRpb246IGZvcm0tZGF0YTsgbmFtZT0idXBsb2FkRmlsZTIiOyBmaWxlbmFtZT0iIg0KQ29udGVudC1UeXBlOiBhcHBsaWNhdGlvbi9
vY3RldC1zdHJlYW0NCg0KDQotLS0tLS1XZWJLaXRGb3JtQm91bmRhcnlEUDZaMXFIUVN6QjZQZjhjLS0NCg==`,
isBase64Encoded: true
}

app.register(multipart, { attachFieldsToBody: true })
app.post('/test', async (request, reply) => {
t.equal(request.body.uploadFile1.fieldname, 'uploadFile1')
t.equal(request.body.uploadFile1.filename, 'test.txt')
t.equal(request.body.uploadFile1.encoding, '7bit')
t.equal(request.body.uploadFile1.mimetype, 'text/plain')
t.equal(Buffer.from(request.body.uploadFile1._buf).toString('utf-8'), 'Hello World!')
reply.send({ hello: 'world' })
})
const proxy = awsLambdaFastify(app, { serializeLambdaArguments: true })
const ret = await proxy(evt)
t.equal(ret.statusCode, 200)
t.equal(ret.body, '{"hello":"world"}')
t.equal(ret.isBase64Encoded, false)
})

test('should parse the multipart form-data successfully given utf8 encoded form data', async (t) => {
t.plan(7)

const app = fastify()
const evt = {
version: '2.0',
httpMethod: 'POST',
path: '/test',
headers: {
'content-type': 'multipart/form-data; boundary=xYzZY'
},
body: '--xYzZY\r\nContent-Disposition: form-data; name="html"\r\n\r\n<p>Hello World</p>\r\n--xYzZY--\r\n',
isBase64Encoded: false
}

app.register(multipart, { attachFieldsToBody: true })
app.post('/test', async (request, reply) => {
console.log(request.body)
t.equal(request.body.html.fieldname, 'html')
t.equal(request.body.html.encoding, '7bit')
t.equal(request.body.html.mimetype, 'text/plain')
t.equal(request.body.html.value, '<p>Hello World</p>')
reply.send({ hello: 'world' })
})
const proxy = awsLambdaFastify(app, { serializeLambdaArguments: true })
const ret = await proxy(evt)
t.equal(ret.statusCode, 200)
t.equal(ret.body, '{"hello":"world"}')
t.equal(ret.isBase64Encoded, false)
})

0 comments on commit e274f7e

Please sign in to comment.