Skip to content

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

License

Notifications You must be signed in to change notification settings

clayreimann/superagent-mock

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NPM Downloads Build Status

Installation | Usage | Supported Methods | Tests | Credits | License

superagent-mock

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+)/',

    /**
     * returns the data
     *
     * @param match array Result of the resolution of the regular expression
     * @param params object sent by 'send' function
     */
    fixtures: function (match, params) {
      /**
       * example: 
       *   request.get('https://error.example/404').end(function(err, res){
       *     console.log(err); // 404
       *   }) 
       */ 
      if (match[1] === '404') {
        throw new Error(404);
      }

      /**
       * example: 
       *   request.get('https://error.example/200').end(function(err, res){
       *     console.log(res.body); // "Data fixtures"
       *   })
       */

      /**
       * example: 
       *   request.get('https://domain.send.example/').send({superhero: "me"}).end(function(err, res){
       *     console.log(res.body); // "Data fixtures - superhero:me"
       *   }) 
       */
      if(params["superhero"]) {
        return 'Data fixtures - superhero:' + params["superhero"];
      } else {
        return 'Data fixtures';
      }
    },

    /**
     * returns the result of the request
     *
     * @param match array Result of the resolution of the regular expression
     * @param data  mixed 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);

Supported Methods

GET, POST and PUT requests are mocked.

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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%