简体   繁体   中英

Simulate HTTP responses to test a node.js HTTP client

How can I simulate HTTP responses to test an app?

What I'd like to do is to simulate HTTP responses to test a HTTP node.js client. Is there something I can use like fakeweb but for node.js?

Why not use a node.js server? They're trivial to write and would give you complete control over the response.

Here's an example, straight from http://nodejs.org/

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');

We we're previously using a custombuilt HTTP dummy server, but recently switched to this:

https://github.com/pgte/nock

If I understood well the question you could use nock ( https://github.com/pgte/nock ) to mock a fake HTTP response from a given route.

An example if you use should.js( https://github.com/visionmedia/should.js/ ) for assertion, request for requests (lol) could be something like this:

should = require 'should'
nock = require 'nock'
request = require 'request'
require '../../app.coffee'

// just a mock obj to reply
mockObj = 
    id: 42

mockRoute = nock 'http://www.mock.com.br'
    .get '/path/you/want/to/mock'
    .reply 200, mockObj

describe "Mock API", ->

    describe "Mock route", ->

        it "should return 200 OK and mockObj.id 42", (done) ->
        // Arranges test URL
        testUrl = "http://www.testurl.com.br:3000/path/you/want/to/test"
        expectedObj =
            id: 42

    request.get testUrl, (error, response, body) ->

        // compares expected response's statusCode with received
        response.should.have.status 200
            expected.should.eql JSON.parse(body)

It's just an example and a bit poor for a production test but I think it's a good example to show how it would work.

But in this case it's like I'm testing if my which is running on port 3000 is being able to handle the request for testUrl and transforming this request on a request to the mocked route which is something outside I want to reach and be sure that node is getting it when I call testUrl.

Hope it helps.

Edit: The code is in coffeescript (in case somebody doesn't know it). Gist in JS if you prefer -> https://gist.github.com/marcoagner/9501662

Edit2: Did not even look up the date.. Gotta sleep :P Sorry. Will just let it here in case it helps somebody out there. hahaha

我自己没有尝试过,但是我发现这是一个模块,它说的确如此: https//github.com/howardabrams/node-mocks-http

There are various tools and mechanisms described here:

http://wiki.apache.org/httpd/WatchingHttpHeaders

Actually, that page is focussed primarily on watching the http conversation in progress. I'd suggest trying either netcat or cadaver for generating your own custom requests.

Short example of netcat usage:

$ printf "GET\n\n" |nc localhost 80
<html><body><h1>It works!</h1></body></html>

EDIT Doh! I just realised you want to simulate responses not requests , sorry. Netcat can still be useful for this, though. Set it up to listen and you have complete control over its output.

For Ruby, if you want to use outside of rails, you might look at the code for Functional testing included with rails as example, which allows a nice set of wrappers around test::unit for accessing http methods. For either node.js or ruby you can use cucumber to test workflows and have a webdriver access the pages. Cucumber works with the web driver of your choice (webrat, selenium, celerity, waiter, etc) and has access to the HTTP request, response, and headers.

http://cukes.info

Expresso provides this by extending the assert module:

assert.response(server, {
    url: '/',
    method: 'GET'
},{
    body: '{"name":"tj"}',
    status: 200,
    headers: {
        'Content-Type': 'application/json; charset=utf8',
        'X-Foo': 'bar'
    }
});

This works with the built-in node.js http server. It has the advantage of not requiring a listening server. The disadvantage is perhaps that it requires you to use the rest of the expresso testing framework.

http://visionmedia.github.com/expresso/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM