如果请求未匹配到规则,则,Xhr Mock 会尝试使用
window.RealXMLHttpRequest
对象发送真实请求window.RealXMLHttpRequest
值会在XhrMock.setup()
的时候自动更改为window.XMLHttpRequest
// 使用回调
Xhr.get('https://api.xxx.com/login', function(req, resp, callback) {
client.incrby('player-login-times', 1, function(err, times) {
if (err) {
return callback(err);
}
callback(
undefined,
resp.json({
success: true
})
);
});
});
// 使用Promise
Xhr.get('https://api.xxx.com/login', function(req, resp) {
return client.incrbyAsync('player-login-times', 1).then(function(times) {
return Promise.resolve(
resp.json({
success: true
})
);
});
});
This repo is a mono-repo managed by lernajs
.
The documentation for the main NPM package can be found here.
Install the dependencies:
yarn
yarn run bootstrap
Build and test the package:
cd packages/xhr-mock
yarn run build # transpile the sources
yarn run test # run the unit tests
Test the package against a few well known XHR libraries:
# NOTE: you need to build the main package first
cd packages/xhr-mock-tests
yarn run test # run the integration tests
Contributors are very welcome! Please raise an issue or PR on Github.