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
|
'use strict'
var http = require('http')
var request = require('../index')
var tape = require('tape')
function runTest (t, options, index) {
var server = http.createServer(function (req, res) {
if (index === 0 || index === 3) {
t.equal(req.headers['content-type'], 'application/x-www-form-urlencoded')
} else {
t.equal(req.headers['content-type'], 'application/x-www-form-urlencoded; charset=UTF-8')
}
t.equal(req.headers['content-length'], '21')
t.equal(req.headers.accept, 'application/json')
var data = ''
req.setEncoding('utf8')
req.on('data', function (d) {
data += d
})
req.on('end', function () {
t.equal(data, 'some=url&encoded=data')
res.writeHead(200)
res.end('done')
})
})
server.listen(0, function () {
var url = 'http://localhost:' + this.address().port
var r = request.post(url, options, function (err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.equal(body, 'done')
server.close(function () {
t.end()
})
})
if (!options.form && !options.body) {
r.form({some: 'url', encoded: 'data'})
}
})
}
var cases = [
{
form: {some: 'url', encoded: 'data'},
json: true
},
{
headers: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'},
form: {some: 'url', encoded: 'data'},
json: true
},
{
headers: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'},
body: 'some=url&encoded=data',
json: true
},
{
// body set via .form() method
json: true
}
]
cases.forEach(function (options, index) {
tape('application/x-www-form-urlencoded ' + index, function (t) {
runTest(t, options, index)
})
})
|