eugef / node-mocks-http

Mock 'http' objects for testing Express routing functions
Other
753 stars 133 forks source link

'end' event is not being triggered #162

Closed amitguptagwl closed 6 years ago

amitguptagwl commented 6 years ago
    it('should call post handler after main handler', done => {

       var request  = httpMocks.createRequest({
            method: 'GET',
            url: '/without/pre/handlers'
        });
        var response = httpMocks.createResponse({
            eventEmitter: require('events').EventEmitter
          });

        router.lookup(request,response);

        response.on('end', function() {
            expect(response._getData()).toEqual(" main -> post.");
            expect(response.statusCode ).toEqual(200);
            expect(response._isEndCalled()).toBe(true);
            done();
        });
    });

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL

eugef commented 6 years ago

Try to move router.lookup(request,response); below the response.on - event might be fired before you assign an event handler

amitguptagwl commented 6 years ago

Thanks. I've changed the code to following

    it('should call post handler after main handler', done => {

       var request  = httpMocks.createRequest({
            method: 'POST',
            url: '/without/pre/handlers'
        });
        var response = httpMocks.createResponse({
            eventEmitter: require('events').EventEmitter
          });

        response.on('end', function() {
            expect(response._getData()).toEqual(" main -> post.");
            expect(response.statusCode ).toEqual(200);
            expect(response._isEndCalled()).toBe(true);
            done();
        });
        router.lookup(request,response);
    });

Now it is calling response 'end' event. But it's not calling request's 'data' or 'end' event

eugef commented 6 years ago

Are you sure request object has those events? I've checked here https://nodejs.org/docs/latest-v8.x/api/http.html#http_class_http_clientrequest and can't see that request has 'data' or 'end' events

amitguptagwl commented 6 years ago

Yes. if I run the server and hit the URL directly. I get the expected response. Which means all the events are being registered successfully at least in the case of direct hit.

In above example, I've also tried with 'body' property. However it is not necessary. Is there anything else I'm missing in above example that I should do?

amitguptagwl commented 6 years ago

In addition, I'm using node v9.5.0, and npm v5.6.0. I'm using find-my-way as router

eugef commented 6 years ago

Could you give an example of your code with subscribing for 'data' or 'end' request events

amitguptagwl commented 6 years ago

Here is the link : https://github.com/muneem4node/muneem/blob/master/src/routesMapper.js

Since it is big, it may eat your valuable time.

amitguptagwl commented 6 years ago

Is there any luck?

I've recently created a new router अनुमार्गक (anumargak) but it didn't with that as well. Everything is fine when I run the server and hit the URL.

amitguptagwl commented 6 years ago

I've just noticed that it doesn't matter which router I'm using, it doesn't call any event on request body.


function fakeRouter(req,res){
    req.on("data", chunk => {
        console.log(chunk);
    });
    res.end("kill me");
}

So in above code, it doesn't prints chunk. I tried to call request.write() but that seems invalid.

amitguptagwl commented 6 years ago

I got a partial solution. I need to emit the event myself after calling the lookup or method.

req.emit('data', 'Hello data!');
req.emit('end');

I was expecting it to be the part of the code.