diff --git a/tests/server.js b/tests/server.js index f18c01d1e..658b18e58 100644 --- a/tests/server.js +++ b/tests/server.js @@ -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' diff --git a/tests/test-json-request.js b/tests/test-json-request.js index cc3a72fc4..d841b1668 100644 --- a/tests/test-json-request.js +++ b/tests/test-json-request.js @@ -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, @@ -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() }) })