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
|
'use strict'
var server = require('./server')
var request = require('../index')
var tape = require('tape')
var s = server.createServer()
tape('setup', function (t) {
s.listen(0, function () {
t.end()
})
})
function testJSONValue (testId, value) {
tape('test ' + testId, function (t) {
var testUrl = '/' + testId
s.on(testUrl, server.createPostJSONValidator(value, 'application/json'))
var opts = {
method: 'PUT',
uri: s.url + testUrl,
json: true,
body: value
}
request(opts, function (err, resp, body) {
t.equal(err, null)
t.equal(resp.statusCode, 200)
t.deepEqual(body, value)
t.end()
})
})
}
function testJSONValueReviver (testId, value, reviver, revivedValue) {
tape('test ' + testId, function (t) {
var testUrl = '/' + testId
s.on(testUrl, server.createPostJSONValidator(value, 'application/json'))
var opts = {
method: 'PUT',
uri: s.url + testUrl,
json: true,
jsonReviver: reviver,
body: value
}
request(opts, function (err, resp, body) {
t.equal(err, null)
t.equal(resp.statusCode, 200)
t.deepEqual(body, revivedValue)
t.end()
})
})
}
function testJSONValueReplacer (testId, value, replacer, replacedValue) {
tape('test ' + testId, function (t) {
var testUrl = '/' + testId
s.on(testUrl, server.createPostJSONValidator(replacedValue, 'application/json'))
var opts = {
method: 'PUT',
uri: s.url + testUrl,
json: true,
jsonReplacer: replacer,
body: value
}
request(opts, function (err, resp, body) {
t.equal(err, null)
t.equal(resp.statusCode, 200)
t.deepEqual(body, replacedValue)
t.end()
})
})
}
testJSONValue('jsonNull', null)
testJSONValue('jsonTrue', true)
testJSONValue('jsonFalse', false)
testJSONValue('jsonNumber', -289365.2938)
testJSONValue('jsonString', 'some string')
testJSONValue('jsonArray', ['value1', 2, null, 8925.53289, true, false, ['array'], { object: 'property' }])
testJSONValue('jsonObject', {
trueProperty: true,
falseProperty: false,
numberProperty: -98346.34698,
stringProperty: 'string',
nullProperty: null,
arrayProperty: ['array'],
objectProperty: { object: 'property' }
})
testJSONValueReviver('jsonReviver', -48269.592, function (k, v) {
return v * -1
}, 48269.592)
testJSONValueReviver('jsonReviverInvalid', -48269.592, 'invalid reviver', -48269.592)
testJSONValueReplacer('jsonReplacer', -48269.592, function (k, v) {
return v * -1
}, 48269.592)
testJSONValueReplacer('jsonReplacerInvalid', -48269.592, 'invalid replacer', -48269.592)
testJSONValueReplacer('jsonReplacerObject', {foo: 'bar'}, function (k, v) {
return v.toUpperCase ? v.toUpperCase() : v
}, {foo: 'BAR'})
tape('missing body', function (t) {
s.on('/missing-body', function (req, res) {
t.equal(req.headers['content-type'], undefined)
res.end()
})
request({url: s.url + '/missing-body', json: true}, function () {
t.end()
})
})
tape('cleanup', function (t) {
s.close(function () {
t.end()
})
})
|