Question

Having recently developed an interest in NodeJS, I finally managed to get my hands on a NodeJS book. A simple example on the book was to create an express server and write a test to verify a simple API exposed as part of the exercise.

Please refer to the following code (I am using node v0.8.8)

The Server (nodeExample.js)

     var express = require('express')
     var app = express.createServer()
     app.listen(8000)
     var msgs = []

     app.get('/', function(req, res) {
        res.send('Welcome to Node Twitter')
     })

     app.post('/send', express.bodyParser(), function(req, res) {
        if (req.body && req.body.tweet) {
           msgs.push(req.body.tweet)
           res.send({status:"ok", message:"Message received"})
        } else {
            res.send({status:"nok", message:"Message not received"})
        }
    })

    app.get('/tweets', function(req,res) {
    res.send(msgs)
    })

The Test

var http = require('http'),
assert = require('assert')
var opts = {
  host: 'localhost',
  port: 8000,
  path: '/send',
  method: 'POST',
  headers: {'content-type':'application/x-www-form-urlencoded'}
}

var req = http.request(opts, function(res) {
 res.setEncoding('utf8')
 var data = "";
res.on('data', function(d) {
  data += d
})
res.on('end', function() {
  assert.strictEqual(data, {status:"ok", message:"Message received"})
 })
})

req.write('tweet=test')
req.end()

I managed to get the server up and running without issues ( Except for a warning that the createServer method of express is deprecated , although doesn't seem to be what's bothering me ) ; but when I run the test, assert throws an exception indicating test failing, which should obviously not happen.

Here's a screenshot of the problem

enter image description here

If you observe the error closely it seems to be the escape characters "\n" appended in parts of the response, that seem to be causing the problem.

I tried to circumvent the issue by

  1. trying different equality checks (equal , strictEqual and deepEqual)
  2. removing the utf8 encoding from the response

but had no luck ( unless I ofcourse perform an equality check with stringified test data, against the stringified response object returned where the formatting has been manually removed ).

So the question is,

  1. Why is the response formatted in the above manner ? Was it only for readability ?
  2. Assuming I am not allowed to remove the formatting, how can I perform an assert test on the response data I just received.
Was it helpful?

Solution

The reply you get from your server is a JSON string:

'{
    "status": "ok",
    "message": "Tweet received"
}'

You are comparing this to an object:

{status:"ok", message:"Message received"}

Since I gather you want to assert you get the correct reply, I suggest you JSON.parse the reply first, and compare the two objects with assert.deepEqual:

assert.deepEqual(JSON.parse(data), {status:"ok", message:"Message received"})

This is probably a wiser way than comparing a string to an object.

OTHER TIPS

Two things:

res.on('end', function(){
  assert.deepEqual(JSON.parse(data), {"status": "ok", "message": "Tweet received"});
});

if (req.body && req.body.tweet) {
    tweets.push(req.body.tweet);
    console.log("success:" + req.body.tweet);
    res.send({"status": "ok", "message": "Tweet received"});
} else {...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top