Question

I'm writing a simple REST service in Node.js (just experimenting), trying to figure out if Node has matured enough yet. I'm also using NodeUnit for my unit testing.

Now, NodeUnit works fine as a testing framework for testing GET-requests, using the HttpUtils, however, testing POST-requests doesn't seem to be obvious.

Testing GET looks like this:

exports.testHelloWorld = function(test) {
    test.expect(1);
    httputil(app.cgi(), function(server, client) {
        client.fetch('GET', '/', {}, function (resp) {
            test.equals('hello world'), resp.body);
            test.done();
        });
    });
}

But how do I test POST-requests? I can change 'GET' to 'POST' and try to write something to 'client', however this doesn't work before .fetch is called because there's no connection yet. And it doesn't work in the .fetch callback function either, because at that time the request has already been executed.

I've looked into the nodeunit code, and there doesn't seem to be support for POSTing data at the moment. So here's my questions:

  • What does it take to test POST-requests?
  • Should I even test POST-requests in a unit test, or does that fall under an integration test and I should use another approach?
Was it helpful?

Solution

You could try this library instead of nodeunit: https://github.com/masylum/testosterone

It's built specifically to test web apps over http.

OTHER TIPS

I've just written this library for testing HTTP servers with nodeunit:

https://github.com/powmedia/nodeunit-httpclient

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top