Using MOCHA to Test Response Header Customized Message

2,807 views
Skip to first unread message

JPJen

unread,
Apr 23, 2014, 3:40:03 PM4/23/14
to moc...@googlegroups.com
  

 

I am using the Express framework. The task is to retrieve a collection from the database and insert a customized message in the response header to the client (the Node.js code shown below works and I can see my customized message in the returning header):

if (req.accepts('json')) {
    res.header('Warning', 'my_customized_message');
    res.header('Content-Type', 'application/json');
    res.send(res.locals.items, 200);

 }

Now, I try to use "mocha" to unit test this line of newly added code using MOCHA:

request = require('request');
should = require('should');

describe('GET /core/dbq/534e930204dd311822ec1c9d', function() {
    this.timeout(15000);
    it ('Check header message', function(done) {
        request.get('http://localhost:3001/ecrud/v1/core/dbq/534e930204dd311822ec1c9d', function(err, response, header) {
            response.statusCode.should.equal(200);                                   //This line passes the MOCHA test
            response.warning.should.equal('my_customized_message');   // Uncaught TypeError: Cannot read property 'should' of undefined
            done();
        } )
    } )
} )

If I test the response.statusCode only, there is no problem.  The MOCHA test passes successfully.  But, if I test the response.warning, I get an error saying the property 'should' is not defined  ( I have run npm should --save-dev).  It looks that "warning" is not a field of the "response".

The same error happens if I try to test the  response.contentType.  I thought "Content-Type" is a very well known response header.  Anyway, what I am really interested in is to test the "Warning" header.  Please help.  Thank you.



 

Miroslav Bajtoš

unread,
Apr 24, 2014, 2:04:52 AM4/24/14
to moc...@googlegroups.com
On Wednesday, April 23, 2014 9:40:03 PM UTC+2, JPJen wrote:
  
request = require('request');
should = require('should');

describe('GET /core/dbq/534e930204dd311822ec1c9d', function() {
    this.timeout(15000);
    it ('Check header message', function(done) {
        request.get('http://localhost:3001/ecrud/v1/core/dbq/534e930204dd311822ec1c9d', function(err, response, header) {
            response.statusCode.should.equal(200);                                   //This line passes the MOCHA test
            response.warning.should.equal('my_customized_message');   // Uncaught TypeError: Cannot read property 'should' of undefined
            done();
        } )
    } )
} )


The third argument of the request callback is body, not header. It does not matter in your test though.

If I test the response.statusCode only, there is no problem.  The MOCHA test passes successfully.  But, if I test the response.warning, I get an error saying the property 'should' is not defined  ( I have run npm should --save-dev).  It looks that "warning" is not a field of the "response".

The same error happens if I try to test the  response.contentType.  I thought "Content-Type" is a very well known response header.  Anyway, what I am really interested in is to test the "Warning" header.  Please help.  Thank you.

You are correct, HTTP headers are not set on the reponse object itself. This is the code to access "warning" header: response.headers.warning.


I personally prefer supertest for testing express applications, you might want to give it a try:


Miroslav

JPJen

unread,
Apr 24, 2014, 1:10:07 PM4/24/14
to moc...@googlegroups.com

Thank you very much for your explanation.  I have learned a lot.  The code is working now.  I am going to learn supertest.

Caroline Jen

unread,
Apr 29, 2014, 11:08:49 AM4/29/14
to moc...@googlegroups.com
Hi Miroslav,

     Thank you very much for helping me last week.  Your advice works. 

     Pursuant to your suggestion, I am re-writing my MOCHA code using "SuperTest" and run into a problem.  Would you mind helping me again?


I have a code snippet of MOCHA that tests the response headers.  The code (below) works.

    request = require('request');
    should = require('should');

    describe('GET /core/dbq/534e930204dd311822ec1c9d', function() {
        this.timeout(15000);
        it ('Check header message', function(done) {

            request.get('http://localhost:3001/ecrud/v1/core/dbq/534e930204dd311822ec1c9d', function(err, response, body) {
                response.statusCode.should.equal(200);
                response.headers.warning.should.equal('100 Max Record Limit Exceeded');
                done();
            } )
        } )
    } )

I am trying to do the same thing using the SuperTest.  I worked on the code for quite a while and managed to make the code bug free. But, my code does not seem to do any comparison of the "expected" response to the "actual" response -- I got 0 passing (0ms) on the console.  I cannot figure out where the problem is.  Therefore, I post my code below for advices.  Thank you.

    var express = require('express');
    var request = require('supertest');
    var app = require('../bin/server.js');

    module.exports = exports = function(request) {


        describe('GET /core/dbq/534e930204dd311822ec1c9d', function() {
            this.timeout(15000);
            it ('Check header message', function(done) {

                 request(app)
                 .get('http://localhost:3001/ecrud/v1/core/dbq/534e930204dd311822ec1c9d')
                 .expect(status, 200)
                 .expect('warning', '100 Max Record Limit Exceeded')
                 .end(function(err, res) {
                     if (err) return done(err);
                     done()
                 });
            } );
        } )
    }





--
You received this message because you are subscribed to a topic in the Google Groups "Mocha" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mochajs/4D4Dly0uPn0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mochajs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages