Domanda

I am wondering if you have experienced the situation where the 'nock' module intercepts the http request but stops without any logging...at least, that appears to me. I may be wrong. Here is the test:

...
suite('Successful SAVE: ', function(){
    setup(function(){
        domain = new Domain({attrOne:1, attrTwo:2});
        minDriver = nock('http://localhost:3002')
            .log(console.log)
            .filteringRequestBody(function(path) {
                return '*';
            })
            .post('/minnehaha/api/review/', '*')
            .reply(201, {ok: true, id: ID, version: VER});
    });

    suite('saving domain', function(){
...
        test('saving review...', function(done){
             //the domain module makes http POST request
             domain.save(function(err){
                if(err){
                    console.log("Error saving: " + err.message);
                }
            });
            done();
        });

...

As you see, the logging for Nock is turned on but it doesn't log anything. The HTTP request made by the module in test - domain is intercepted, meaning it doesn't reach the actual server, however. The res.on('data' or 'end') is not called from the request in the module:

http = require 'http'

class Domain
    ...    
    save: (fn) ->

            post_data = JSON.stringify(@)

            options =
              host: "localhost"
              port: 3002
              path: "/minnehaha/api/review/"
              method: "POST"
              headers:
                'Content-Type': "application/json"
                'Content-Length': post_data.length

            req = http.request options, (res) ->
              body = ''
              res.setEncoding 'utf8'

              res.on 'data', (chunk) ->
                body += chunk

              res.on 'end', () ->
                newReview = JSON.parse(body)
                return fn null


            req.on 'error', (e) ->
              console.log 'problem with POST Review request to controller: ' + e.message

            req.write post_data + '\n'

module.exports  = Domain

This module works(reaches the server and the response is processed by the module) if the Nock intercept is turned off. With that, i conclude the issue is the nock intercept. Furthermore, it works if the HTTP POST request is made from the test file itself, but not from the module.... The test file is in JS but module in CoffeeScript... that shouldn't matter? Any help appreciated

È stato utile?

Soluzione

The issue was that when using Nock, it is necessary to explicitly end the request stream as following:

   req.write(post_data + '\n');
   req.end();

Without the 'req.end()', the Nock intercepter will never emit the 'end' event on the response stream making it hang.

For those new to CoffeeScript like myself, it is not enough

req.end 

This only compiles to a reference of the 'end' function instead of calling it. To make function call, this works:

req.end '\n'

This compiles to 'req.end('\n') and calls the 'end' function

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top