Giter VIP home page Giter VIP logo

mock-request's Introduction

NOTICE: THIS PROJECT HAS BEEN DEPRECATED

We suggest using nock: http://github.com/flatiron/nock

mock-request

A simple testing tool for mocking HTTP sequences of request / response pairs in node.js

Installation

Installing npm (node package manager)

  $ curl http://npmjs.org/install.sh | sh

Installing mock-request

  $ [sudo] npm install mock-request

Purpose

The mock-request library is designed to easily mock HTTP endpoints in tests which fit the following boilerplate:

  1. Tests are performed by making HTTP requests against an API server
  2. Assertions are made against the HTTP response (status, headers, body, etc)
  3. Rinse. Repeat.

The method signature used by the mock-request library matches that of the popular request library widely used in the node.js community. Besides that method signature, there are no external test framework dependencies so use whatever your preference is: vows, expresso, nodeunit, etc.

If you're curious why mocking your HTTP requests could be helpful you can read up here.

Usage

The mock-request library is designed for explicit mocking, it does not perform any interpolation or guessing beyond assuming a default response of 200 unless otherwise indicated. Here's a sample of how to use mock-request:

  var mockRequest = require('mock-request'),
      assert = require('assert');
      
  var mockFn = mockRequest.mock()
                .get('/not-here')
                .respond(404)
                .post('/tests', { 'some': 'test-youre-running' }, {
                  'x-test-header': true
                })
                .respond(200, { 'some': 'test-has-completed' }, {
                  'x-test-completed': true
                })
                .run();
  
  //
  // The mock function returned from `mockRequest.mock()` is 
  // synchronous because no HTTP actually takes place.
  //
  mockFn({
    method: 'GET',
    uri: 'http://mock-request/not-here'
  }, function (err, res, body) {
    assert.equal(res.statusCode, 404);
  });
  
  //
  // Now that we've made the first `GET` request, we have to make the 
  // `POST` request or `mock-request` will throw an `Error`
  //
  mockFn({
    method: 'POST',
    uri: 'http://mock-request/tests',
    headers: {
      'some': 'test-youre-running'
    }
  }, function (err, res, body) {
    assert.deepEqual(body, { 'some': 'test-has-completed' });
    assert.deepEqual(res.headers, { 'x-test-completed': true });
    
    //
    // We mocked this request to respond with 404, so by asserting
    // 200, this will throw an `AssertionError`.
    //
    assert.equal(res.statusCode, 200);
  });

Roadmap

  1. Get feedback on what else could be exposed through this library.
  2. Improve it.
  3. Repeat (1) + (2).

Run Tests

  npm test

Contributors: Dominic Tarr

mock-request's People

Contributors

indexzero avatar coderarity avatar yawnt avatar

Watchers

Sobin George Thomas avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.