Skip to content

Commit

Permalink
Updated test to verify JSON responses
Browse files Browse the repository at this point in the history
  • Loading branch information
apoco committed Nov 25, 2014
1 parent aac4a9a commit d12b4e6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
18 changes: 18 additions & 0 deletions tests/server.js
Expand Up @@ -77,6 +77,24 @@ exports.createPostValidator = function (text, reqContentType) {
}
return l
}
exports.createPostJSONValidator = function (value, reqContentType) {
var l = function (req, resp) {
var r = ''
req.on('data', function (chunk) {r += chunk})
req.on('end', function () {
var parsedValue = JSON.parse(r)
assert.deepEqual(parsedValue, value)
if (reqContentType) {
assert.ok(req.headers['content-type'])
assert.ok(~req.headers['content-type'].indexOf(reqContentType))
}
resp.writeHead(200, {'content-type':'application/json'})
resp.write(JSON.stringify({ status: 'OK', value: parsedValue }))
resp.end()
})
}
return l
}
exports.createGetResponse = function (text, contentType) {
var l = function (req, resp) {
contentType = contentType || 'text/plain'
Expand Down
4 changes: 3 additions & 1 deletion tests/test-json-request.js
Expand Up @@ -16,7 +16,7 @@ tape('setup', function(t) {
function testJSONValue(testId, value) {
tape('test ' + testId, function(t) {
var testUrl = '/' + testId
s.on(testUrl, server.createPostValidator(JSON.stringify(value), 'application/json'))
s.on(testUrl, server.createPostJSONValidator(value, 'application/json'))
var opts = {
method: 'PUT',
uri: s.url + testUrl,
Expand All @@ -25,6 +25,8 @@ function testJSONValue(testId, value) {
}
request(opts, function (err, resp, body) {
t.equal(err, null)
t.equal(body.status, 'OK')
t.deepEqual(body.value, value)
t.end()
})
})
Expand Down

0 comments on commit d12b4e6

Please sign in to comment.