node-interceptor

HTTP & HTTPS request interceptor that allows to define which fixture (JSON object) to return, inspired by jQuery.fixture, thegreatape/fakeweb and ppcano/fixtures.

HTTP & HTTPS request interceptor. Allows to define which fixture (JSON object) to return for each request, inspired by jQuery.fixture, thegreatape/fakeweb and ppcano/fixtures.

Installation

npm install node-interceptor

Testing

git clone git://github.com/dearwish/node-interceptor.git
cd node-interceptor
npm install
npm test

Examples

1. Registers a list of interception rules to spoof HTTPS requests to Facebook Graph API:

// Setup the <b>Interceptor</b> on HTTP and HTTPS protocols.
var interceptor = require('node-interceptor'),
    // Fixtures are already preloaded
    fixtures = interceptor.fixtures,
    https = require('https');

https.Interceptor.registerAll([{ 
    // properties checked to qualify a request for interception
    test: {
        // uri can be a string or regular expression
        uri: '/' + process.env.FACEBOOK_APP_ID,
        host: 'graph.facebook.com'
    },
    // request property names to ignore
    ignored: ["headers"],

    // response properties to be returned
    response: {
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify(fixtures.app),
        statusCode: 200
    }}, {
    test: {
        // uri can be a regular expression (starts with "/me" in this case)
        uri: /^\/me/,
        host: 'graph.facebook.com'
    },
    response: {
        body: JSON.stringify(fixtures.me)
    }}, {
    test: {
        uri: '/you',
        host: 'graph.facebook.com'
    },
    response: {
        headers: {'Content-Type': 'text/plain'},
        body: 'Unknown API call attempt!',
        statusCode: 404
    }
}]);
// This time an HTTP request
var http = require('http');

http.Interceptor.register({
    test: {
        uri: '/me/friends',
        host: 'graph.facebook.com'
    },

    ignored: ["headers"],

    response: {
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify(fixtures.friends),
        statusCode: 200
    }
});
http.Interceptor.unregister({
    // The test is required and it should match the registration rule.test
    test: {
        uri: '/me',
        host: 'graph.facebook.com'
    }
});
http.Interceptor.unregisterAll();

// OR

http.Interceptor.clear();

Providing a list of headers that will be compared using deep-equal with the actual request headers.

rule: {
    test: {
        headers: {'Accept': '*/*', 'Content-Type': 'application/json'}
        uri: '/me/friends',
        host: 'graph.facebook.com'
    },
 ...
rule: {
    response: {
        headers: {
            'Set-Cookie': 'AuthSessId=41D3D0110BA61CB171B345F147C089BD; path=/',
            'Content-Type': 'application/json'
        }
        uri: /^\/dialog\/oauth/,
        host: 'www.facebook.com'
    },
 ...
{
    response: {
        headers: {'Content-Type': 'application/json'},
        statusCode: 200,
        body: ''
    },
    ignored: ["headers"]
};
var interceptor = require('node-interceptor'),
    Interceptor = interceptor.Interceptor,
    ...;

var defaults = Interceptor.getDefaults();
defaults.ignored = ["sweeties"];
Interceptor.setDefaults(defaults);
Interceptor.addDefaults({
    response: {
        headers: {'Content-Type': 'text/html', 'Accept': "*/*"},
        statusCode: 204
    },
    ignored: ["headers", "footers"]
});

Miscellaneous

  • Added Interceptor class
  • Both http and https have their own instance of Interceptor
  • Added status code to response
  • Added defaults configuration as static + API to modify them
  • Some bug fixes
  • Initial version - forked from node-fakeweb
  • Added HTTPS support
  • Integrated with fixtures (using node-fixtures module)
  • Some bug fixes
  1. Add Connect-like uri patterns mapping (i.e. "/users/:id/edit"). Currently it can be done using the regular expressions only.
  2. Add fixtures integration - the response messages will be read from .js or .json file that reside in application fixtures directory. Inspired by ppcano's fixtures.
  3. Add dynamic fixtures similar to jQuery.fixture.
  4. Add configuration on initialization
    • to enable control over setup of interceptor on different protocols
    • to give the ability not to initialize the fixtures

MIT