1.0.5 • Published 9 years ago

superagent-mock-okb v1.0.5

Weekly downloads
2
License
MIT
Repository
github
Last release
9 years ago

superagent-mock Build Status

superagent plugin allowing to simulate HTTP calls by returning data fixtures based on the requested URL.

Note: this plugin is developed for superagent: ^v1.1.0.

See this post to know why we use superagent-mock at M6Web.

Installation

Install with npm:

$ npm install superagent-mock

Usage

First, you have to define the URLs to mock in a configuration file:

// ./superagent-mock-config.js file
module.exports = [
  {
    // regular expression of URL
    pattern: 'https://domain.example/(\\w+)/',

    // callback that returns the data
    fixtures: function () {
      return 'Data fixtures';
    },

    // `match`: result of the resolution of the regular expression
    // `data`: data returns by `fixtures` attribute
    callback: function (match, data) {
      return {
        body: data
      };
    }
  },
  ...
];

Then use the plugin:

// ./server.js file
var request = require('superagent');
var config = require('./superagent-mock-config');

require('superagent-mock')(request, config);

Tests

To run units tests: npm test.

To check code style: npm run lint.

Credits

Developped by the Cytron Team of M6 Web.
Tested with nodeunit.

License

superagent-mock is licensed under the MIT license.

1.0.5

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago