Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored multipart attach body test #164

Merged
merged 1 commit into from
Oct 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 55 additions & 54 deletions test/multipart-attach-body.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ const http = require('http')
const path = require('path')
const fs = require('fs')
const stream = require('stream')
const { once } = require('events')
const pump = util.promisify(stream.pipeline)

const filePath = path.join(__dirname, '../README.md')

test('should be able to attach all parsed fields and files and make it accessible through "req.body"', function (t) {
test('should be able to attach all parsed fields and files and make it accessible through "req.body"', async function (t) {
t.plan(6)

const fastify = Fastify()
Expand All @@ -36,37 +37,37 @@ test('should be able to attach all parsed fields and files and make it accessibl
reply.code(200).send()
})

fastify.listen(0, async function () {
// request
const form = new FormData()
const opts = {
protocol: 'http:',
hostname: 'localhost',
port: fastify.server.address().port,
path: '/',
headers: form.getHeaders(),
method: 'POST'
}
await fastify.listen(0)

// request
const form = new FormData()
const opts = {
protocol: 'http:',
hostname: 'localhost',
port: fastify.server.address().port,
path: '/',
headers: form.getHeaders(),
method: 'POST'
}

const req = http.request(opts, (res) => {
t.equal(res.statusCode, 200)
res.resume()
res.on('end', () => {
t.pass('res ended successfully')
})
})
form.append('upload', fs.createReadStream(filePath))
form.append('hello', 'world')
const req = http.request(opts)
form.append('upload', fs.createReadStream(filePath))
form.append('hello', 'world')

try {
await pump(form, req)
} catch (error) {
t.error(error, 'formData request pump: no err')
}
})
try {
await pump(form, req)
} catch (error) {
t.error(error, 'formData request pump: no err')
}

const [res] = await once(req, 'response')
t.equal(res.statusCode, 200)
res.resume()
await once(res, 'end')
t.pass('res ended successfully')
})

test('should be able to define a custom "onFile" handler', function (t) {
test('should be able to define a custom "onFile" handler', async function (t) {
t.plan(7)

const fastify = Fastify()
Expand Down Expand Up @@ -94,34 +95,34 @@ test('should be able to define a custom "onFile" handler', function (t) {
reply.code(200).send()
})

fastify.listen(0, async function () {
// request
const form = new FormData()
const opts = {
protocol: 'http:',
hostname: 'localhost',
port: fastify.server.address().port,
path: '/',
headers: form.getHeaders(),
method: 'POST'
}
await fastify.listen(0)

// request
const form = new FormData()
const opts = {
protocol: 'http:',
hostname: 'localhost',
port: fastify.server.address().port,
path: '/',
headers: form.getHeaders(),
method: 'POST'
}

const req = http.request(opts, (res) => {
t.equal(res.statusCode, 200)
res.resume()
res.on('end', () => {
t.pass('res ended successfully')
})
})
form.append('upload', fs.createReadStream(filePath))
form.append('hello', 'world')
const req = http.request(opts)
form.append('upload', fs.createReadStream(filePath))
form.append('hello', 'world')

try {
await pump(form, req)
} catch (error) {
t.error(error, 'formData request pump: no err')
}
})
try {
await pump(form, req)
} catch (error) {
t.error(error, 'formData request pump: no err')
}

const [res] = await once(req, 'response')
t.equal(res.statusCode, 200)
res.resume()
await once(res, 'end')
t.pass('res ended successfully')
})

test('should not process requests with content-type other than multipart', function (t) {
Expand Down