@natlibfi/http-client-x-mock

A mock for http-client-x


Keywords
mock, fake, stub, spy, test, http, xmlhttprequest, fetch, get, head, delete, post, put, rest, network, request, client, browser, node, cross-platform
License
LGPL-3.0+
Install
npm install @natlibfi/http-client-x-mock@1.0.1

Documentation

HTTP client X Mock NPM Version Build Status Test Coverage

A mock for HTTP client X. You can use the module to mock HTTP calls made by HTTP client X, XMLHttpRequest or Node.js's http/https modules.

Usage

The module exports an object which has the following properties:

  • create: This function creates a mock for a HTTP call. It either takes an object for an argument or multiple arguments: url (string), method (string), status (number), headers (object), body (string).
  • restore: Restores original behaviour by removing all mocks set up with create.

Default options for create (url is mandatory):

{
  status: 200,
  method: 'GET',
  headers: {},
  body: ''
}

Node.js

var http_mock = require('http-client-x-mock/lib/nodejs/main');

http_mock.create({
  url: 'http://foo.bar',
  method: 'GET',
  status: 200,
  headers: {
    'X-Foo': 'bar'
  },
  'foobar'  
});

http_mock.restore();

AMD

define(['http-client-x-mock/lib/browser/main'], function(http_mock) {

  http_mock.create({
    url: 'http://foo.bar',
    method: 'GET',
    status: 200,
    headers: {
      'X-Foo': 'bar'
    },
    'foobar'    
  });

  http_mock.restore();

});

Development

Clone the sources and install the package using npm:

npm install

Run the following NPM script to lint, test and check coverage of the code:

npm run check

License and copyright

Copyright (c) 2016 University Of Helsinki (The National Library Of Finland)

This project's source code is licensed under the terms of GNU General Public License Version 3 or any later version.