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
|
'use strict'
var http = require('http')
var request = require('../index')
var tape = require('tape')
function runTest (t, options) {
var server = http.createServer(function (req, res) {
var data = ''
req.setEncoding('utf8')
req.on('data', function (d) {
data += d
})
req.on('end', function () {
if (options.qs) {
t.equal(req.url, '/?rfc3986=%21%2A%28%29%27')
}
t.equal(data, options._expectBody)
res.writeHead(200)
res.end('done')
})
})
server.listen(0, function () {
var port = this.address().port
request.post('http://localhost:' + port, options, function (err, res, body) {
t.equal(err, null)
server.close(function () {
t.end()
})
})
})
}
var bodyEscaped = 'rfc3986=%21%2A%28%29%27'
var bodyJson = '{"rfc3986":"!*()\'"}'
var cases = [
{
_name: 'qs',
qs: {rfc3986: "!*()'"},
_expectBody: ''
},
{
_name: 'qs + json',
qs: {rfc3986: "!*()'"},
json: true,
_expectBody: ''
},
{
_name: 'form',
form: {rfc3986: "!*()'"},
_expectBody: bodyEscaped
},
{
_name: 'form + json',
form: {rfc3986: "!*()'"},
json: true,
_expectBody: bodyEscaped
},
{
_name: 'qs + form',
qs: {rfc3986: "!*()'"},
form: {rfc3986: "!*()'"},
_expectBody: bodyEscaped
},
{
_name: 'qs + form + json',
qs: {rfc3986: "!*()'"},
form: {rfc3986: "!*()'"},
json: true,
_expectBody: bodyEscaped
},
{
_name: 'body + header + json',
headers: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'},
body: "rfc3986=!*()'",
json: true,
_expectBody: bodyEscaped
},
{
_name: 'body + json',
body: {rfc3986: "!*()'"},
json: true,
_expectBody: bodyJson
},
{
_name: 'json object',
json: {rfc3986: "!*()'"},
_expectBody: bodyJson
}
]
var libs = ['qs', 'querystring']
libs.forEach(function (lib) {
cases.forEach(function (options) {
options.useQuerystring = (lib === 'querystring')
tape(lib + ' rfc3986 ' + options._name, function (t) {
runTest(t, options)
})
})
})
|