Luc
Luc

Reputation: 17072

How to get response's body with mocha / node.js

I'm very new to mocha / omf. I have the basic test below:

omf('http://localhost:7000', function(client) {
  client.get('/apps', function(response){
    response.has.statusCode(200);
    response.has.body('["test1","test2"]');
  });
});

I'd like to check if the value "test2" is among the list returned but I cannot figure out how this is feasible. I'm thinking of something like:

omf('http://localhost:7000', function(client) {
  client.get('/apps', function(response){
    response.has.statusCode(200);
    // response.body.split.contains("test2"); // Something like that
  });
});

Can I access response.body and then parse the string ?

** UPDATE **

I've tried to test with mocha, just a simple status code:

request = require("request");

describe('Applications API', function(){
  it('Checks existence of test application', function(done){
    request
      .get('http://localhost:7000/apps')
      .expect(200, done);
  });
});

but I got the following error:

TypeError: Object # has no method 'expect'

Any idea ? Does mocha need to have additional addons ?

Upvotes: 2

Views: 11129

Answers (1)

Pascal Belloncle
Pascal Belloncle

Reputation: 11389

The second example can not work as shown. request.get is asynchronous.

Here's a working example running with request and should

request = require("request");
should = require("should");

describe('Applications API', function() {
  it('Checks existence of test application', function(done) {
    request.get('http://google.com', function(err, response, body) {
      response.statusCode.should.equal(200);
      body.should.include("I'm Feeling Lucky");
      done();
    })
  });
});

Upvotes: 8

Related Questions