-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtest-qq-multipart.js
61 lines (57 loc) · 1.23 KB
/
test-qq-multipart.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
let test = require('tape')
let fs = require('fs')
let path = require('path')
let tiny = require('./dist.js')
let http = require('http')
let port = 3000
let host = 'localhost'
let server
test('make a multipart post', t=> {
t.plan(1)
t.ok(tiny, 'got env')
})
test('start a fake server', t=> {
t.plan(1)
// somebody thought this was intuitive
server = http.createServer((req, res)=> {
let body = []
req.on('data', chunk => body.push(chunk))
req.on('end', function _end() {
body = Buffer.concat(body).toString()
res.end(body)
})
})
server.listen({port, host}, err=> {
if (err) t.fail(err)
else t.pass(`Started server`)
})
})
test('can multipart/form-data post', t=> {
t.plan(1)
let file = fs.readFileSync(path.join(__dirname, 'readme.md'))
tiny.post({
url: `http://${host}:${port}`,
headers: {
'content-type': 'multipart/form-data'
},
data: {
one: 1,
file
}
},
function _post(err, data) {
if (err) {
t.fail(err, err)
console.log(err)
}
else {
t.ok(data.body.includes(file.toString()), 'posted')
console.log(data)
}
})
})
test('close fake server', t=> {
t.plan(1)
server.close()
t.ok(true, 'server closed')
})