requestmock
Module for mocking request module
Last updated 2 years ago by resin.io .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install requestmock 
SYNC missed versions from official npm registry.

requestmock

npm version dependencies

Mock calls to request module. Compatible with request-promise.

Installation

$ npm install requestmock

Using with mockery

const requestMock = require('requestmock');
const mockery = require('mockery');

mockery.enable({ warnOnUnregistered: false });
mockery.registerMock('request', requestMock);

// make sure request is required after mockery is set up.
const request = require('request');

requestMock.register('get', 'http://google.com', function (opts, cb) {
	cb(null, { statusCode: 200 }, 'get mock');
});
request('http://google.com', function (err, response, body) {
	console.log(body == 'get mock');
});

Using with request-promise

Using requestMock with request-promise requires you to either rely on the global Promise library or specify a Promise library explicitly as shown below:

const requestMock = require('requestmock');
requestMock.configure({ Promise: require('some-promise-library') });

Documentation

  • requestmock(opts, cb)

The module exports a function that can be used in the same way as the request module.

By default, it forwards all calls to the request module. Otherwise, you can use the module's functions to register or deregister handlers based on url and method.

This is meant to be used in conjunction with mockery, to allow some http requests to be mocked and others to be executed normally, during testing.

  • .register(method, url, handler)

Setup a handler for a url. The handler will take two parameters, opts and callback, which are the same that were passed to the request module. The url can be either a string (representing an exact url) or a RegExp.

The handler should call the callback function passing an error object (null if success), a response-like object, and a string (http body).

The response-like object should define the properties as needed by your modules, typically at least the statusCode property.

  • .deregister([ method ], url)

Remove handler for given method and url. If method is omitted, all handlers are removed.

  • .log(enabled)

Enable or disable logging of all requests. Useful for inspecting all the outgoing requests of an application.

Support

If you're having any problem, please raise an issue on GitHub and the Resin.io team will be happy to help.

Tests

Run the test suite by doing:

$ npm install && npm test

Contribute

Before submitting a PR, please make sure that you include tests, and that coffeelint runs without any warning.

License

The project is licensed under the MIT license.

Current Tags

  • 1.4.0                                ...           latest (2 years ago)

19 Versions

  • 1.4.0                                ...           2 years ago
  • 1.3.2                                ...           2 years ago
  • 1.3.1                                ...           2 years ago
  • 1.3.0                                ...           2 years ago
  • 1.2.1                                ...           2 years ago
  • 1.1.1                                ...           2 years ago
  • 1.1.0                                ...           2 years ago
  • 1.0.2                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
  • 0.2.0                                ...           3 years ago
  • 0.0.9                                ...           5 years ago
  • 0.0.8                                ...           5 years ago
  • 0.0.7                                ...           5 years ago
  • 0.0.6                                ...           5 years ago
  • 0.0.5                                ...           5 years ago
  • 0.0.4                                ...           5 years ago
  • 0.0.3                                ...           5 years ago
  • 0.0.2                                ...           5 years ago
  • 0.0.1                                ...           5 years ago
Downloads
Today 0
This Week 19
This Month 19
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (6)
Dependents (2)

Copyright 2014 - 2016 © taobao.org |