Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

requests should be event emitters #17

Closed
focusaurus opened this issue Sep 5, 2014 · 8 comments
Closed

requests should be event emitters #17

focusaurus opened this issue Sep 5, 2014 · 8 comments

Comments

@focusaurus
Copy link

I need to mock doing req.on('end', onEnd); and the mock request class does not have the event emitter methods.

@estilles
Copy link

estilles commented Mar 7, 2015

@focusaurus, the mockRequest implementation in this project aims to mock Express' request class and Node's http.IncomingMessage (which Express' request extends), neither of which are event emitters.

It's possible you believe mockRequest mocked Node's http.request, which is not the case.

If you have any additional questions or comments feel free to reopen this issue.

@estilles estilles closed this as completed Mar 7, 2015
@focusaurus
Copy link
Author

neither of which are event emitters.

Node's http.IncomingMessage subclasses Stream.readable as seen here.

Stream.readable inherits from stream, which is an event emitter as seen here

In the repl, you can see that http.IncomingMessage clearly has an "on" function.

new http.IncomingMessage().on
[Function]

Streaming in node is built upon the event emitter pattern, and HTTP messages support streaming.

@estilles estilles removed the invalid label Mar 7, 2015
@estilles
Copy link

estilles commented Mar 7, 2015

@focusaurus, you're absolutely right. My apologies. Let me see if I can work on implementing this.

@estilles estilles reopened this Mar 7, 2015
@estilles estilles added this to the v2.0 milestone Mar 8, 2015
@estilles estilles mentioned this issue Mar 17, 2015
@estilles
Copy link

We're scheduling this feature for our v2.0 release.

@jvwing
Copy link
Contributor

jvwing commented Jul 15, 2015

I worked around this issue by mashing an instance of node-memorystream with node-mock-http's mockRequest. The mockRequest is then a stream, complete with event support sufficient to fool body-parser. The end() method is used to flush the stream.

Underscore hackery aside, this seems like a reasonable approach to supporting request-as-stream. Would a pull request for adding stream support based on node-memorystream be welcomed?

var assert = require("assert");
var bodyParser = require("body-parser");
var httpMocks = require("node-mocks-http");
var MemoryStream = require("memorystream");
var _ = require("underscore");

describe("Testing form post with body-parser", function () {
    var urlEncodedParser = bodyParser.urlencoded({ "extended": false, "limit": "32kb" });
    var memStream = new MemoryStream();
    var httpMocksRequest = httpMocks.createRequest({
        "method": "POST",
        "headers": {
            "content-type": "application/x-www-form-urlencoded",
            "transfer-encoding": "chunked"
        }
    });
    var mockRequest = _.extend(memStream, httpMocksRequest);
    var mockResponse = httpMocks.createResponse();
    var nextCalled = false;
    before(function () {
        urlEncodedParser(mockRequest, mockResponse, function () {
            nextCalled = true;
        });
        mockRequest.end("name=Some+Dude&email=some.dude%40foobar.com");
    });
    it("Called Next", function () {
        assert.equal(nextCalled, true);
    });
    it("Parsed Body", function () {
        assert.equal(mockRequest.body.name, "Some Dude");
        assert.equal(mockRequest.body.email, "some.dude@foobar.com");
    });
});

@evanshortiss
Copy link
Contributor

Hey,

I came across this also and just added the following in mockRequest.js:

var events = require('events');
var mockRequest = Object.create(events.EventEmitter.prototype);

This allows me to fire events on the mockRequest in test cases. Happy to submit a PR.

@evanshortiss
Copy link
Contributor

Here's a sample commit link with the change.

@github-actions
Copy link

Stale issue message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants