node-http-intercept

1.0.7 • Public • Published

http-intercept

Intercepts node http.request. Allows conditional request modification.

Install

Install with npm

$ npm i http-intercept --save-dev

Usage

  const httpIntercept = require('http-intercept');
  const options = {
    hostname: 'google.com',
    port: 80,
    path: '/upload',
    method: 'get',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  };
 
  const interceptOptions = {
    searchValue: 'google',
    replaceValue: 'bing',
    callback: (newOptions) => {
      // newOptions.hostname ==='bing.com
    },
  };
 
  httpIntercept(interceptOptions);
 
  http.request(options);
  const httpIntercept = require('http-intercept');
  const options = {
    hostname: 'google.com',
    port: 80,
    path: '/upload',
    method: 'get',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  };
 
  const interceptOptions = {
    searchValue: 'google',
    replaceValue: 'bing',
    callback: (newOptions) => {
      // newOptions is unchanged because predicate fails.
    },
    predicate: () => false,
  };
 
  httpIntercept(interceptOptions);
 
  http.request(options);
  const httpIntercept = require('http-intercept');
  const options = {
    hostname: 'google.com',
    port: 80,
    path: '/upload',
    method: 'get',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  };
 
  const interceptOptions = {
    searchAndReplaceValues: [{
      google: 'bing',
    },
    {
      '.com': '.org',
    },
    ],
    callback: (newOptions) => {
      // hostname is now 'bing.org'
    },
  };
 
  httpIntercept(interceptOptions);
 
  http.request(options);

Running tests

Install dev dependencies:

$ npm i -d && npm test

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue

Author

Jeff Wilde

License

Copyright © 2017 Jeff Wilde Licensed under the (MIT OR Apache-2.0) license.


This file was generated by readme-generator on January 26, 2017.

Readme

Keywords

Package Sidebar

Install

npm i node-http-intercept

Weekly Downloads

5

Version

1.0.7

License

(MIT OR Apache-2.0)

Last publish

Collaborators

  • gofreddo