XMLHttpRequest Mock
Mock for XMLHttpRequest that works both in Browser and Node.js and can override globals
Usage
Manual installation
Clone the sources and install the package (In the source directory) on command line using npm
:
npm install
Testing
Run the following NPM script to lint, test and check coverage of the code:
npm run check
AMD
;
Node.js
var xhrMockFactory = ; xhr_mock = XMLHttpRequestMock = xhr_mock; /** * @internal Do something with XMLHttpRequestMock **/ ... /** * @internal Override global XMLHttpRequestMock **/ xhr_mock = ; xhr_mock; /** * @internal Run external tests that use XMLHttpRequest **/ ... xhr_mock; });
Configuration
The factory function takes a single argument which (If not undefined) will override the global XMLHttpRequest constructor. The object produced by the factory has the following methods available:
- create: Creates a mock of XMLHttpRequest constructor and returns it. Also overrides the global constructor if override was requested when creating the mock object. Optional parameters object can be passed as an argument:
- status [Number]: HTTP response status code
- body [String]: Response body (Will be set to the responseText-property in the xhr instance)
- headers [Object]: Headers to set
- restore: Returns the original XMLHttpRequest constructor or undefined if not available. Also restore the global constructor if override was requested when creating the mock object.
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.