npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-interceptor

v0.0.2

Published

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

Downloads

12

Readme

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:

This example is using the node-fixtures module for reading JSON objects from <your-app>/test/fixtures/*.js or *.json.

Using registerAll which accepts an Array of interception rules and registers them one by one.

// 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([{ 

Mandatory

    // 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'
    },

Optional (with defaults)

    // 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
    }}, {

Using regular expression for URI matching:

    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)
    }}, {

Using plain text as output body:

    test: {
        uri: '/you',
        host: 'graph.facebook.com'
    },
    response: {
        headers: {'Content-Type': 'text/plain'},
        body: 'Unknown API call attempt!',
        statusCode: 404
    }
}]);

Using register to register a single interception rule.

// 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
    }
});

Using unregister to unregister a single interception rule.

http.Interceptor.unregister({
    // The test is required and it should match the registration rule.test
    test: {
        uri: '/me',
        host: 'graph.facebook.com'
    }
});

Using unregisterAll or clear to unregister all interception rules.

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'
    },
 ...

Providing a list of custom headers that will be sent to the response.

rule: {
    response: {
        headers: {
            'Set-Cookie': 'AuthSessId=41D3D0110BA61CB171B345F147C089BD; path=/',
            'Content-Type': 'application/json'
        }
        uri: /^\/dialog\/oauth/,
        host: 'www.facebook.com'
    },
 ...

2. Sets defaults for all Interceptor instances

Default values defined in Interceptor are:

{
    response: {
        headers: {'Content-Type': 'application/json'},
        statusCode: 200,
        body: ''
    },
    ignored: ["headers"]
};

API to override default values

Using getDefaults and setDefaults functions:

var interceptor = require('node-interceptor'),
    Interceptor = interceptor.Interceptor,
    ...;

var defaults = Interceptor.getDefaults();
defaults.ignored = ["sweeties"];
Interceptor.setDefaults(defaults);

Using addDefaults to add more defaults to existing ones (defaults with the same name will be replaced):

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

node-interceptor also provides a nodeunit test case that resets the uri intercept list in between tests. See tests/suits/testcase.js for an example.

Miscellaneous

Change log

version 0.0.2

  • 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

version 0.0.1

  • Initial version - forked from node-fakeweb
  • Added HTTPS support
  • Integrated with fixtures (using node-fixtures module)
  • Some bug fixes

Future enhancements

  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

License

MIT