-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathmultipart-body-schema.test.js
137 lines (116 loc) · 3.16 KB
/
multipart-body-schema.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict'
const util = require('util')
const test = require('tap').test
const FormData = require('form-data')
const Fastify = require('fastify')
const multipart = require('..')
const http = require('http')
const path = require('path')
const fs = require('fs')
const stream = require('stream')
const pump = util.promisify(stream.pipeline)
const filePath = path.join(__dirname, '../README.md')
test('should be able to use JSON schema to validate request', function (t) {
t.plan(6)
const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))
fastify.register(multipart, { attachFieldsToBody: true, sharedSchemaId: '#mySharedSchema' })
const original = fs.readFileSync(filePath, 'utf8')
fastify.post('/', {
schema: {
body: {
type: 'object',
required: ['hello', 'upload'],
properties: {
hello: { $ref: '#mySharedSchema' },
upload: { $ref: '#mySharedSchema' }
}
}
}
}, async function (req, reply) {
t.ok(req.isMultipart())
t.same(Object.keys(req.body), ['upload', 'hello'])
const content = await req.body.upload.toBuffer()
t.equal(content.toString(), original)
t.equal(req.body.hello.value, 'world')
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'
}
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')
try {
await pump(form, req)
} catch (error) {
t.error(error, 'formData request pump: no err')
}
})
})
test('should throw because JSON schema is invalid', function (t) {
t.plan(2)
const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))
fastify.register(multipart, { attachFieldsToBody: true, sharedSchemaId: '#mySharedSchema' })
fastify.post('/', {
schema: {
body: {
type: 'object',
required: ['hello'],
properties: {
hello: {
properties: {
value: {
type: 'string',
enum: ['red']
}
}
}
}
}
}
}, async function (req, reply) {
console.log(req.body)
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'
}
const req = http.request(opts, (res) => {
t.equal(res.statusCode, 400)
res.resume()
res.on('end', () => {
t.pass('res ended successfully')
})
})
form.append('hello', 'world')
try {
await pump(form, req)
} catch (error) {
t.error(error, 'formData request pump: no err')
}
})
})