Node.js testing RESTful API (vows.js?)

后端 未结 3 881
深忆病人
深忆病人 2021-02-15 15:14

I could really do with some advice on testing a RESTful api I created in node.js. There are a plethora of frameworks out there and I am at a loss. My testing knowledge isn\'t go

相关标签:
3条回答
  • 2021-02-15 15:44

    I have used vowsjs and request libraries.

    I have found them to be the easiest as both libs are documented properly and seem to be actively developed and maintained. (I haven't found the docs for APIeasy to be sufficient.)

    Here's an example test I am writing at the moment to test Couchapp's HTTP API:

    var request = require ('request')
        , vows = require ('vows')
        , assert = require ('assert');
    
    var BASE_URL = "http://local.todos.com:5984/todos/"
        , HEADERS = {
          'Content-Type': 'application/json'
        }
        , revisionReference;
    
    vows.describe ('CouchApp Todos REST API')
      // --------------------------------------------
      // Testing PUTs
      // ============================================
      .addBatch ({
        "A PUT to /todos/test-host without data": {
          topic : function () {
            request ({
              uri: BASE_URL + "test-host",
              method: 'PUT',
              headers: HEADERS
            }, this.callback );
          } 
          , "should respond with 201" : function ( err, res, body ) {
            assert.equal ( res.statusCode, 201 );
          }
          , "should have an id 'test-host'" : function ( err, res, body ) {
            assert.equal ( JSON.parse( res.body )._id, 'test-host' );
          }
          , "response should contain empty todos []" : function ( err, res, body ) {
            assert.include ( JSON.parse( res.body ), 'todos' );
            assert.deepEqual ( JSON.parse( res.body ).todos, [] );
          }
        }
      })
      .addBatch ({
        "A PUT to /todos/test-host with one todo item (an object)" : {
            topic : function () {
              request ({
                uri: BASE_URL + "test-host"
                , body: JSON.stringify({
                    "title" : "Testing Todo",
                    "isDone" : false
                  })
                , method : "PUT"
                , headers : HEADERS
              }, this.callback );
            }
            , "should respond with 201" : function ( err, res, body ) {
              assert.equal ( res.statusCode, 201 );
            }
            , "should have an id 'test-host'" : function ( err, res, body ) {
              assert.equal ( JSON.parse( res.body )._id, 'test-host' )
            }
            , "response should contain todos array with one item" : function ( err, res, body ) {
              assert.include ( JSON.parse( res.body ), 'todos' );
              assert.deepEqual ( 
                JSON.parse( res.body ).todos
                , [{
                  "title" : "Testing Todo",
                  "isDone" : false,
                  "_id" : 0
                }] 
              );
            }
        }
      })
    
    0 讨论(0)
  • 2021-02-15 15:47

    http://blog.nodejitsu.com/rest-easy-test-any-api-in-nodejs is designed for this very purpose. It's a DSL that sits on top of Vows that streamlines the process of writing out tests using vows.

    Basic test:

    //
    // Here we will configure our tests to use 
    // http://localhost:8080 as the remote address
    // and to always send 'Content-Type': 'application/json'
    //
    suite.use('localhost', 8000)
         .setHeader('Content-Type', 'application/json');
         //
         // A GET Request to /ping
         //   should respond with 200
         //   should respond with { pong: true }
         //
         .get('/ping')
           .expect(200, { pong: true })
         //
         // A POST Request to /ping
         //   should respond with 200
         //   should respond with { dynamic_data: true }
         //
         .post('/ping', { dynamic_data: true })
           .expect(200, { dynamic_data: true })
    
    0 讨论(0)
  • 2021-02-15 15:48

    Update 6 months later

    vows-is sucks. use mocha

    Original

    Updated with vow-is code

    Here's the vows-is example from the vows-is examples folder.

    // simple HTTP
    // Run with node example/simple-http.js
    
    var express = require("express");
        is = require("../src/vows-is.js");
    
    is.config({
        "server": {
            "factory": function _factory(cb) {
                var app = express.createServer();
    
                app.get("/", function(req, res) {
                    res.send("hello world");
                })
    
                app.listen(3000);
    
                cb(app);
            },
            "uri": "http://localhost:3000",
            "kill": function _kill(app) {
                app.close();
            }
        }
    });
    
    is.suite("http request test").batch()
    
        .context("a request to GET /")
            .topic.is.a.request("GET /")
            .vow.it.should.have.status(200)
            .vow.it.should.have
                .header("content-type", "text/html; charset=utf-8")
            .context("contains a body that")
                .topic.is.property('body')
                .vow.it.should.be.ok
                .vow.it.should.include.string("hello world")
    
    .suite().run({
        reporter: is.reporter
    }, function() {
        console.log("finished");
        is.end();
    })
    

    This uses vows-is.

    0 讨论(0)
提交回复
热议问题