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
|
'use strict'
var server = require('./server')
var request = require('../index')
var tape = require('tape')
var s = server.createServer()
function runTest (name, test) {
tape(name, function (t) {
s.on('/' + name, test.resp)
request(s.url + '/' + name, test, function (err, resp, body) {
t.equal(err, null)
if (test.expectBody) {
if (Buffer.isBuffer(test.expectBody)) {
t.equal(test.expectBody.toString(), body.toString())
} else {
t.deepEqual(test.expectBody, body)
}
}
t.end()
})
})
}
tape('setup', function (t) {
s.listen(0, function () {
t.end()
})
})
runTest('testGet', {
resp: server.createGetResponse('TESTING!'),
expectBody: 'TESTING!'
})
runTest('testGetChunkBreak', {
resp: server.createChunkResponse(
[ Buffer.from([239]),
Buffer.from([163]),
Buffer.from([191]),
Buffer.from([206]),
Buffer.from([169]),
Buffer.from([226]),
Buffer.from([152]),
Buffer.from([131])
]),
expectBody: '\uf8ff\u03a9\u2603'
})
runTest('testGetBuffer', {
resp: server.createGetResponse(Buffer.from('TESTING!')),
encoding: null,
expectBody: Buffer.from('TESTING!')
})
runTest('testGetJSON', {
resp: server.createGetResponse('{"test":true}', 'application/json'),
json: true,
expectBody: {'test': true}
})
runTest('testPutString', {
resp: server.createPostValidator('PUTTINGDATA'),
method: 'PUT',
body: 'PUTTINGDATA'
})
runTest('testPutBuffer', {
resp: server.createPostValidator('PUTTINGDATA'),
method: 'PUT',
body: Buffer.from('PUTTINGDATA')
})
runTest('testPutJSON', {
resp: server.createPostValidator(JSON.stringify({foo: 'bar'})),
method: 'PUT',
json: {foo: 'bar'}
})
runTest('testPutMultipart', {
resp: server.createPostValidator(
'--__BOUNDARY__\r\n' +
'content-type: text/html\r\n' +
'\r\n' +
'<html><body>Oh hi.</body></html>' +
'\r\n--__BOUNDARY__\r\n\r\n' +
'Oh hi.' +
'\r\n--__BOUNDARY__--'
),
method: 'PUT',
multipart: [ {'content-type': 'text/html', 'body': '<html><body>Oh hi.</body></html>'},
{'body': 'Oh hi.'}
]
})
tape('cleanup', function (t) {
s.close(function () {
t.end()
})
})
|