axios-mock-adapter
Advanced tools
Comparing version 1.6.1 to 1.7.0
@@ -61,14 +61,15 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
var verbs = ['get', 'post', 'head', 'delete', 'patch', 'put']; | ||
var VERBS = ['get', 'post', 'head', 'delete', 'patch', 'put']; | ||
function adapter() { | ||
return function(config) { | ||
var mockAdapter = this; | ||
// axios >= 0.13.0 only passes the config and expects a promise to be | ||
// returned. axios < 0.13.0 passes (config, resolve, reject). | ||
if (arguments.length === 3) { | ||
handleRequest.call(this, arguments[0], arguments[1], arguments[2]); | ||
handleRequest(mockAdapter, arguments[0], arguments[1], arguments[2]); | ||
} else { | ||
return new Promise(function(resolve, reject) { | ||
handleRequest.call(this, resolve, reject, config); | ||
}.bind(this)); | ||
handleRequest(mockAdapter, resolve, reject, config); | ||
}); | ||
} | ||
@@ -79,3 +80,3 @@ }.bind(this); | ||
function reset() { | ||
this.handlers = verbs.reduce(function(accumulator, verb) { | ||
this.handlers = VERBS.reduce(function(accumulator, verb) { | ||
accumulator[verb] = []; | ||
@@ -110,32 +111,11 @@ return accumulator; | ||
MockAdapter.prototype.onAny = function onAny(matcher, body) { | ||
var _this = this; | ||
return { | ||
reply: function reply(code, response, headers) { | ||
var handler = [matcher, code, response, headers, body]; | ||
verbs.forEach(function(verb) { | ||
_this.handlers[verb].push(handler); | ||
}); | ||
return _this; | ||
}, | ||
replyOnce: function replyOnce(code, response, headers) { | ||
var handler = [matcher, code, response, headers, body]; | ||
_this.replyOnceHandlers.push(handler); | ||
verbs.forEach(function(verb) { | ||
_this.handlers[verb].push(handler); | ||
}); | ||
return _this; | ||
} | ||
}; | ||
}; | ||
verbs.forEach(function(method) { | ||
VERBS.concat('any').forEach(function(method) { | ||
var methodName = 'on' + method.charAt(0).toUpperCase() + method.slice(1); | ||
MockAdapter.prototype[methodName] = function(matcher, body) { | ||
var _this = this; | ||
var matcher = matcher === undefined ? /.*/ : matcher; | ||
return { | ||
reply: function reply(code, response, headers) { | ||
var handler = [matcher, code, response, headers, body]; | ||
_this.handlers[method].push(handler); | ||
var handler = [matcher, body, code, response, headers]; | ||
addHandler(method, _this.handlers, handler); | ||
return _this; | ||
@@ -145,6 +125,12 @@ }, | ||
replyOnce: function replyOnce(code, response, headers) { | ||
var handler = [matcher, code, response, headers, body]; | ||
_this.handlers[method].push(handler); | ||
var handler = [matcher, body, code, response, headers]; | ||
addHandler(method, _this.handlers, handler); | ||
_this.replyOnceHandlers.push(handler); | ||
return _this; | ||
}, | ||
passThrough: function passThrough() { | ||
var handler = [matcher, body]; | ||
addHandler(method, _this.handlers, handler); | ||
return _this; | ||
} | ||
@@ -155,2 +141,12 @@ }; | ||
function addHandler(method, handlers, handler) { | ||
if (method === 'any') { | ||
VERBS.forEach(function(verb) { | ||
handlers[verb].push(handler); | ||
}); | ||
} else { | ||
handlers[method].push(handler); | ||
} | ||
} | ||
module.exports = MockAdapter; | ||
@@ -165,23 +161,53 @@ | ||
function handleRequest(resolve, reject, config) { | ||
var url = config.url.slice(config.baseURL ? config.baseURL.length : 0); | ||
var handler = utils.findHandler(this.handlers, config.method, url, config.data); | ||
function makeResponse(result, config) { | ||
return { | ||
status: result[0], | ||
data: result[1], | ||
headers: result[2], | ||
config: config | ||
}; | ||
} | ||
function handleRequest(mockAdapter, resolve, reject, config) { | ||
config.url = config.url.slice(config.baseURL ? config.baseURL.length : 0); | ||
config.adapter = null; | ||
var handler = utils.findHandler(mockAdapter.handlers, config.method, config.url, config.data); | ||
if (handler) { | ||
utils.purgeIfReplyOnce(this, handler); | ||
var response = handler[1] instanceof Function | ||
? handler[1](config) | ||
: handler.slice(1); | ||
utils.purgeIfReplyOnce(mockAdapter, handler); | ||
if (handler.length === 2) { // passThrough handler | ||
mockAdapter | ||
.axiosInstance | ||
.request(config) | ||
.then(resolve, reject); | ||
} else if (!(handler[2] instanceof Function)) { | ||
utils.settle(resolve, reject, makeResponse(handler.slice(2), config), mockAdapter.delayResponse); | ||
} else { | ||
var result = handler[2](config); | ||
if (!(result.then instanceof Function)) { | ||
utils.settle(resolve, reject, makeResponse(result, config), mockAdapter.delayResponse); | ||
} else { | ||
result.then( | ||
function(result) { | ||
utils.settle(resolve, reject, makeResponse(result, config), mockAdapter.delayResponse); | ||
}, | ||
function(error) { | ||
if (mockAdapter.delayResponse > 0) { | ||
setTimeout(function() { | ||
reject(error); | ||
}, mockAdapter.delayResponse); | ||
} else { | ||
reject(error); | ||
} | ||
} | ||
); | ||
} | ||
} | ||
} else { // handler not found | ||
utils.settle(resolve, reject, { | ||
status: response[0], | ||
data: response[1], | ||
headers: response[2], | ||
config: config | ||
}, this.delayResponse); | ||
} else { | ||
utils.settle(resolve, reject, { | ||
status: 404, | ||
config: config | ||
}, this.delayResponse); | ||
}, mockAdapter.delayResponse); | ||
} | ||
@@ -202,3 +228,3 @@ } | ||
function eql(a, b) { | ||
function isEqual(a, b) { | ||
return deepEqual(a, b, { strict: true }); | ||
@@ -221,5 +247,5 @@ } | ||
if (typeof handler[0] === 'string') { | ||
return url === handler[0] && isBodyMatching(body, handler[4]); | ||
return url === handler[0] && isBodyMatching(body, handler[1]); | ||
} else if (handler[0] instanceof RegExp) { | ||
return handler[0].test(url) && isBodyMatching(body, handler[4]); | ||
return handler[0].test(url) && isBodyMatching(body, handler[1]); | ||
} | ||
@@ -237,3 +263,3 @@ }); | ||
} catch (e) { } | ||
return parsedBody ? eql(parsedBody, requiredBody) : eql(body, requiredBody); | ||
return parsedBody ? isEqual(parsedBody, requiredBody) : isEqual(body, requiredBody); | ||
} | ||
@@ -293,2 +319,3 @@ | ||
module.exports = { | ||
find: find, | ||
findHandler: findHandler, | ||
@@ -295,0 +322,0 @@ purgeIfReplyOnce: purgeIfReplyOnce, |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("axios")):"function"==typeof define&&define.amd?define(["axios"],e):"object"==typeof exports?exports.AxiosMockAdapter=e(require("axios")):t.AxiosMockAdapter=e(t.axios)}(this,function(t){return function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return t[r].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e,n){"use strict";function r(){return function(t){return 3!==arguments.length?new Promise(function(e,n){s.call(this,e,n,t)}.bind(this)):void s.call(this,arguments[0],arguments[1],arguments[2])}.bind(this)}function o(){this.handlers=u.reduce(function(t,e){return t[e]=[],t},{}),this.replyOnceHandlers=[]}function i(t,e){o.call(this),t&&(this.axiosInstance=t,this.originalAdapter=t.defaults.adapter,this.delayResponse=e&&e.delayResponse>0?e.delayResponse:null,t.defaults.adapter=r.call(this))}var s=n(1),u=["get","post","head","delete","patch","put"];i.prototype.adapter=r,i.prototype.restore=function(){this.axiosInstance&&(this.axiosInstance.defaults.adapter=this.originalAdapter)},i.prototype.reset=o,i.prototype.onAny=function(t,e){var n=this;return{reply:function(r,o,i){var s=[t,r,o,i,e];return u.forEach(function(t){n.handlers[t].push(s)}),n},replyOnce:function(r,o,i){var s=[t,r,o,i,e];return n.replyOnceHandlers.push(s),u.forEach(function(t){n.handlers[t].push(s)}),n}}},u.forEach(function(t){var e="on"+t.charAt(0).toUpperCase()+t.slice(1);i.prototype[e]=function(e,n){var r=this;return{reply:function(o,i,s){var u=[e,o,i,s,n];return r.handlers[t].push(u),r},replyOnce:function(o,i,s){var u=[e,o,i,s,n];return r.handlers[t].push(u),r.replyOnceHandlers.push(u),r}}}}),t.exports=i},function(t,e,n){function r(t,e,n){var r=n.url.slice(n.baseURL?n.baseURL.length:0),i=o.findHandler(this.handlers,n.method,r,n.data);if(i){o.purgeIfReplyOnce(this,i);var s=i[1]instanceof Function?i[1](n):i.slice(1);o.settle(t,e,{status:s[0],data:s[1],headers:s[2],config:n},this.delayResponse)}else o.settle(t,e,{status:404,config:n},this.delayResponse)}var o=n(2);t.exports=r},function(t,e,n){"use strict";function r(t,e){return p(t,e,{strict:!0})}function o(t,e){for(var n=t.length,r=0;r<n;r++){var o=t[r];if(e(o))return o}}function i(t,e,n,r){return o(t[e.toLowerCase()],function(t){return"string"==typeof t[0]?n===t[0]&&s(r,t[4]):t[0]instanceof RegExp?t[0].test(n)&&s(r,t[4]):void 0})}function s(t,e){if(void 0===e)return!0;var n;try{n=JSON.parse(t)}catch(o){}return n?r(n,e):r(t,e)}function u(t,e){var n=t.replyOnceHandlers.indexOf(e);n>-1&&(t.replyOnceHandlers.splice(n,1),Object.keys(t.handlers).forEach(function(r){n=t.handlers[r].indexOf(e),n>-1&&t.handlers[r].splice(n,1)}))}function a(t,e,n,r){return r>0?void setTimeout(function(){a(t,e,n)},r):n.config&&n.config.validateStatus?void(n.config.validateStatus(n.status)?t(n):e(c("Request failed with status code "+n.status,n.config,n))):void(n.status>=200&&n.status<300?t(n):e(n))}function c(t,e,n){if(!l)return n;var r=new Error(t);return r.config=e,r.response=n,r}var f=n(3),p=n(4),l=!!f.create().defaults.headers;t.exports={findHandler:i,purgeIfReplyOnce:u,settle:a}},function(e,n){e.exports=t},function(t,e,n){function r(t){return null===t||void 0===t}function o(t){return!(!t||"object"!=typeof t||"number"!=typeof t.length)&&("function"==typeof t.copy&&"function"==typeof t.slice&&!(t.length>0&&"number"!=typeof t[0]))}function i(t,e,n){var i,f;if(r(t)||r(e))return!1;if(t.prototype!==e.prototype)return!1;if(a(t))return!!a(e)&&(t=s.call(t),e=s.call(e),c(t,e,n));if(o(t)){if(!o(e))return!1;if(t.length!==e.length)return!1;for(i=0;i<t.length;i++)if(t[i]!==e[i])return!1;return!0}try{var p=u(t),l=u(e)}catch(d){return!1}if(p.length!=l.length)return!1;for(p.sort(),l.sort(),i=p.length-1;i>=0;i--)if(p[i]!=l[i])return!1;for(i=p.length-1;i>=0;i--)if(f=p[i],!c(t[f],e[f],n))return!1;return typeof t==typeof e}var s=Array.prototype.slice,u=n(5),a=n(6),c=t.exports=function(t,e,n){return n||(n={}),t===e||(t instanceof Date&&e instanceof Date?t.getTime()===e.getTime():!t||!e||"object"!=typeof t&&"object"!=typeof e?n.strict?t===e:t==e:i(t,e,n))}},function(t,e){function n(t){var e=[];for(var n in t)e.push(n);return e}e=t.exports="function"==typeof Object.keys?Object.keys:n,e.shim=n},function(t,e){function n(t){return"[object Arguments]"==Object.prototype.toString.call(t)}function r(t){return t&&"object"==typeof t&&"number"==typeof t.length&&Object.prototype.hasOwnProperty.call(t,"callee")&&!Object.prototype.propertyIsEnumerable.call(t,"callee")||!1}var o="[object Arguments]"==function(){return Object.prototype.toString.call(arguments)}();e=t.exports=o?n:r,e.supported=n,e.unsupported=r}])}); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("axios")):"function"==typeof define&&define.amd?define(["axios"],t):"object"==typeof exports?exports.AxiosMockAdapter=t(require("axios")):e.AxiosMockAdapter=t(e.axios)}(this,function(e){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return e[r].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){"use strict";function r(){return function(e){var t=this;return 3!==arguments.length?new Promise(function(n,r){a(t,n,r,e)}):void a(t,arguments[0],arguments[1],arguments[2])}.bind(this)}function o(){this.handlers=u.reduce(function(e,t){return e[t]=[],e},{}),this.replyOnceHandlers=[]}function s(e,t){o.call(this),e&&(this.axiosInstance=e,this.originalAdapter=e.defaults.adapter,this.delayResponse=t&&t.delayResponse>0?t.delayResponse:null,e.defaults.adapter=r.call(this))}function i(e,t,n){"any"===e?u.forEach(function(e){t[e].push(n)}):t[e].push(n)}var a=n(1),u=["get","post","head","delete","patch","put"];s.prototype.adapter=r,s.prototype.restore=function(){this.axiosInstance&&(this.axiosInstance.defaults.adapter=this.originalAdapter)},s.prototype.reset=o,u.concat("any").forEach(function(e){var t="on"+e.charAt(0).toUpperCase()+e.slice(1);s.prototype[t]=function(t,n){var r=this,t=void 0===t?/.*/:t;return{reply:function(o,s,a){var u=[t,n,o,s,a];return i(e,r.handlers,u),r},replyOnce:function(o,s,a){var u=[t,n,o,s,a];return i(e,r.handlers,u),r.replyOnceHandlers.push(u),r},passThrough:function(){var o=[t,n];return i(e,r.handlers,o),r}}}}),e.exports=s},function(e,t,n){function r(e,t){return{status:e[0],data:e[1],headers:e[2],config:t}}function o(e,t,n,o){o.url=o.url.slice(o.baseURL?o.baseURL.length:0),o.adapter=null;var i=s.findHandler(e.handlers,o.method,o.url,o.data);if(i)if(s.purgeIfReplyOnce(e,i),2===i.length)e.axiosInstance.request(o).then(t,n);else if(i[2]instanceof Function){var a=i[2](o);a.then instanceof Function?a.then(function(i){s.settle(t,n,r(i,o),e.delayResponse)},function(t){e.delayResponse>0?setTimeout(function(){n(t)},e.delayResponse):n(t)}):s.settle(t,n,r(a,o),e.delayResponse)}else s.settle(t,n,r(i.slice(2),o),e.delayResponse);else s.settle(t,n,{status:404,config:o},e.delayResponse)}var s=n(2);e.exports=o},function(e,t,n){"use strict";function r(e,t){return p(e,t,{strict:!0})}function o(e,t){for(var n=e.length,r=0;r<n;r++){var o=e[r];if(t(o))return o}}function s(e,t,n,r){return o(e[t.toLowerCase()],function(e){return"string"==typeof e[0]?n===e[0]&&i(r,e[1]):e[0]instanceof RegExp?e[0].test(n)&&i(r,e[1]):void 0})}function i(e,t){if(void 0===t)return!0;var n;try{n=JSON.parse(e)}catch(o){}return n?r(n,t):r(e,t)}function a(e,t){var n=e.replyOnceHandlers.indexOf(t);n>-1&&(e.replyOnceHandlers.splice(n,1),Object.keys(e.handlers).forEach(function(r){n=e.handlers[r].indexOf(t),n>-1&&e.handlers[r].splice(n,1)}))}function u(e,t,n,r){return r>0?void setTimeout(function(){u(e,t,n)},r):n.config&&n.config.validateStatus?void(n.config.validateStatus(n.status)?e(n):t(c("Request failed with status code "+n.status,n.config,n))):void(n.status>=200&&n.status<300?e(n):t(n))}function c(e,t,n){if(!l)return n;var r=new Error(e);return r.config=t,r.response=n,r}var f=n(3),p=n(4),l=!!f.create().defaults.headers;e.exports={find:o,findHandler:s,purgeIfReplyOnce:a,settle:u}},function(t,n){t.exports=e},function(e,t,n){function r(e){return null===e||void 0===e}function o(e){return!(!e||"object"!=typeof e||"number"!=typeof e.length)&&("function"==typeof e.copy&&"function"==typeof e.slice&&!(e.length>0&&"number"!=typeof e[0]))}function s(e,t,n){var s,f;if(r(e)||r(t))return!1;if(e.prototype!==t.prototype)return!1;if(u(e))return!!u(t)&&(e=i.call(e),t=i.call(t),c(e,t,n));if(o(e)){if(!o(t))return!1;if(e.length!==t.length)return!1;for(s=0;s<e.length;s++)if(e[s]!==t[s])return!1;return!0}try{var p=a(e),l=a(t)}catch(d){return!1}if(p.length!=l.length)return!1;for(p.sort(),l.sort(),s=p.length-1;s>=0;s--)if(p[s]!=l[s])return!1;for(s=p.length-1;s>=0;s--)if(f=p[s],!c(e[f],t[f],n))return!1;return typeof e==typeof t}var i=Array.prototype.slice,a=n(5),u=n(6),c=e.exports=function(e,t,n){return n||(n={}),e===t||(e instanceof Date&&t instanceof Date?e.getTime()===t.getTime():!e||!t||"object"!=typeof e&&"object"!=typeof t?n.strict?e===t:e==t:s(e,t,n))}},function(e,t){function n(e){var t=[];for(var n in e)t.push(n);return t}t=e.exports="function"==typeof Object.keys?Object.keys:n,t.shim=n},function(e,t){function n(e){return"[object Arguments]"==Object.prototype.toString.call(e)}function r(e){return e&&"object"==typeof e&&"number"==typeof e.length&&Object.prototype.hasOwnProperty.call(e,"callee")&&!Object.prototype.propertyIsEnumerable.call(e,"callee")||!1}var o="[object Arguments]"==function(){return Object.prototype.toString.call(arguments)}();t=e.exports=o?n:r,t.supported=n,t.unsupported=r}])}); |
{ | ||
"name": "axios-mock-adapter", | ||
"version": "1.6.1", | ||
"version": "1.7.0", | ||
"description": "Axios adapter that allows to easily mock requests", | ||
@@ -9,5 +9,6 @@ "main": "src/index.js", | ||
"test": "mocha", | ||
"test:coverage": "istanbul cover node_modules/.bin/_mocha", | ||
"lint": "eslint src test", | ||
"build:umd": "webpack src/index.js dist/axios-mock-adapter.js", | ||
"build:umd:min": "NODE_ENV=production webpack src/index.js dist/axios-mock-adapter.min.js", | ||
"build:umd:min": "cross-env NODE_ENV=production webpack src/index.js dist/axios-mock-adapter.min.js", | ||
"prepublish": "npm run clean && npm run build:umd && npm run build:umd:min" | ||
@@ -41,8 +42,10 @@ }, | ||
"devDependencies": { | ||
"axios": "^0.13.1", | ||
"axios": "^0.15.0", | ||
"chai": "^3.5.0", | ||
"eslint": "^3.0.1", | ||
"mocha": "^2.4.5", | ||
"cross-env": "^3.0.0", | ||
"eslint": "^3.7.1", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^3.1.2", | ||
"rimraf": "^2.5.2", | ||
"webpack": "^1.12.15" | ||
"webpack": "^1.13.2" | ||
}, | ||
@@ -49,0 +52,0 @@ "dependencies": { |
@@ -16,4 +16,4 @@ # axios-mock-adapter | ||
* https://npmcdn.com/axios-mock-adapter/dist/axios-mock-adapter.js | ||
* https://npmcdn.com/axios-mock-adapter/dist/axios-mock-adapter.min.js | ||
* https://unpkg.com/axios-mock-adapter/dist/axios-mock-adapter.js | ||
* https://unpkg.com/axios-mock-adapter/dist/axios-mock-adapter.min.js | ||
@@ -45,8 +45,5 @@ axios-mock-adapter works on Node as well as in a browser, it works with axios v0.9.0 and above. | ||
}); | ||
// If a request is made for a URL that is not handled in the mock adapter, | ||
// the promise will reject with a response that has status 404. | ||
``` | ||
To add a delay to responses, specify a delay ammount (in milliseconds) when instantiating the adapter | ||
To add a delay to responses, specify a delay amount (in milliseconds) when instantiating the adapter | ||
@@ -98,2 +95,9 @@ ```js | ||
Specify no path to match by verb alone | ||
```js | ||
// Reject all POST requests with HTTP 500 | ||
mock.onPost().reply(500); | ||
``` | ||
Chaining is also supported | ||
@@ -114,3 +118,3 @@ | ||
// Any following request would return a 404 since there are | ||
// no matching handlers | ||
// no matching handlers left | ||
``` | ||
@@ -135,3 +139,4 @@ | ||
mock.onAny(/.*/).reply(config => { | ||
// Match ALL requests | ||
mock.onAny().reply(config => { | ||
const [method, url, ...response] = responses.shift(); | ||
@@ -144,7 +149,81 @@ if (config.url === url && config.method.toUpperCase() === method) return response; | ||
Requests that do not map to a mock handler are rejected with a HTTP 404 response. Since | ||
handlers are matched in order, a final `onAny()` can be used to change the default | ||
behaviour | ||
```js | ||
// Mock GET requests to /foo, reject all others with HTTP 500 | ||
mock | ||
.onGet('/foo').reply(200) | ||
.onAny().reply(500); | ||
``` | ||
Mocking a request with a specific request body/data | ||
```js | ||
// usable with reply, replyOnce, onAny, onPatch, onPost, onPut ... | ||
mock.onPut('/withBody', { request: 'body' }).reply(200); | ||
mock.onPut('/product', { id: 4, name: 'foo' }).reply(204); | ||
``` | ||
`.passThrough()` forwards the matched request over network | ||
```js | ||
// Mock POST requests to /api with HTTP 201, but forward | ||
// GET requests to server | ||
mock | ||
.onPost(/\/^api/).reply(201) | ||
.onGet(/\/^api/).passThrough(); | ||
``` | ||
Recall that the order of handlers is significant | ||
```js | ||
// Mock specific requests, but let unmatched ones through | ||
mock | ||
.onGet('/foo').reply(200) | ||
.onPut('/bar', { xyz: 'abc' }).reply(204) | ||
.onAny().passThrough(); | ||
``` | ||
Note that `passThrough` requests are not subject to delaying by `delayResponse`. | ||
As of 1.7.0, `reply` function may return a Promise: | ||
```js | ||
mock.onGet('/product').reply(function(config) { | ||
return new Promise(function(resolve, reject) { | ||
setTimeout(function() { | ||
if (Math.random() > 0.1) { | ||
resolve([200, { id: 4, name: 'foo' } ]); | ||
} else { | ||
// reject() reason will be passed as-is. | ||
// Use HTTP error status code to simulate server failure. | ||
resolve([500, { success: false } ]); | ||
} | ||
}, 1000); | ||
}); | ||
}); | ||
``` | ||
Composing from multiple sources with Promises: | ||
```js | ||
var normalAxios = axios.create(); | ||
var mockAxios = axios.create(); | ||
var mock = MockAdapter(mockAxios); | ||
mock | ||
.onGet('/orders') | ||
.reply(() => Promise.all([ | ||
normalAxios | ||
.get('/api/v1/orders') | ||
.then(resp => resp.data), | ||
normalAxios | ||
.get('/api/v2/orders') | ||
.then(resp => resp.data), | ||
{ id: '-1', content: 'extra row 1' }, | ||
{ id: '-2', content: 'extra row 2' } | ||
]).then( | ||
sources => [200, sources.reduce((agg, source) => agg.concat(source))] | ||
) | ||
); | ||
``` |
var utils = require('./utils'); | ||
function handleRequest(resolve, reject, config) { | ||
var url = config.url.slice(config.baseURL ? config.baseURL.length : 0); | ||
var handler = utils.findHandler(this.handlers, config.method, url, config.data); | ||
function makeResponse(result, config) { | ||
return { | ||
status: result[0], | ||
data: result[1], | ||
headers: result[2], | ||
config: config | ||
}; | ||
} | ||
function handleRequest(mockAdapter, resolve, reject, config) { | ||
config.url = config.url.slice(config.baseURL ? config.baseURL.length : 0); | ||
config.adapter = null; | ||
var handler = utils.findHandler(mockAdapter.handlers, config.method, config.url, config.data); | ||
if (handler) { | ||
utils.purgeIfReplyOnce(this, handler); | ||
var response = handler[1] instanceof Function | ||
? handler[1](config) | ||
: handler.slice(1); | ||
utils.purgeIfReplyOnce(mockAdapter, handler); | ||
if (handler.length === 2) { // passThrough handler | ||
mockAdapter | ||
.axiosInstance | ||
.request(config) | ||
.then(resolve, reject); | ||
} else if (!(handler[2] instanceof Function)) { | ||
utils.settle(resolve, reject, makeResponse(handler.slice(2), config), mockAdapter.delayResponse); | ||
} else { | ||
var result = handler[2](config); | ||
if (!(result.then instanceof Function)) { | ||
utils.settle(resolve, reject, makeResponse(result, config), mockAdapter.delayResponse); | ||
} else { | ||
result.then( | ||
function(result) { | ||
utils.settle(resolve, reject, makeResponse(result, config), mockAdapter.delayResponse); | ||
}, | ||
function(error) { | ||
if (mockAdapter.delayResponse > 0) { | ||
setTimeout(function() { | ||
reject(error); | ||
}, mockAdapter.delayResponse); | ||
} else { | ||
reject(error); | ||
} | ||
} | ||
); | ||
} | ||
} | ||
} else { // handler not found | ||
utils.settle(resolve, reject, { | ||
status: response[0], | ||
data: response[1], | ||
headers: response[2], | ||
config: config | ||
}, this.delayResponse); | ||
} else { | ||
utils.settle(resolve, reject, { | ||
status: 404, | ||
config: config | ||
}, this.delayResponse); | ||
}, mockAdapter.delayResponse); | ||
} | ||
@@ -25,0 +55,0 @@ } |
@@ -5,14 +5,15 @@ 'use strict'; | ||
var verbs = ['get', 'post', 'head', 'delete', 'patch', 'put']; | ||
var VERBS = ['get', 'post', 'head', 'delete', 'patch', 'put']; | ||
function adapter() { | ||
return function(config) { | ||
var mockAdapter = this; | ||
// axios >= 0.13.0 only passes the config and expects a promise to be | ||
// returned. axios < 0.13.0 passes (config, resolve, reject). | ||
if (arguments.length === 3) { | ||
handleRequest.call(this, arguments[0], arguments[1], arguments[2]); | ||
handleRequest(mockAdapter, arguments[0], arguments[1], arguments[2]); | ||
} else { | ||
return new Promise(function(resolve, reject) { | ||
handleRequest.call(this, resolve, reject, config); | ||
}.bind(this)); | ||
handleRequest(mockAdapter, resolve, reject, config); | ||
}); | ||
} | ||
@@ -23,3 +24,3 @@ }.bind(this); | ||
function reset() { | ||
this.handlers = verbs.reduce(function(accumulator, verb) { | ||
this.handlers = VERBS.reduce(function(accumulator, verb) { | ||
accumulator[verb] = []; | ||
@@ -54,32 +55,11 @@ return accumulator; | ||
MockAdapter.prototype.onAny = function onAny(matcher, body) { | ||
var _this = this; | ||
return { | ||
reply: function reply(code, response, headers) { | ||
var handler = [matcher, code, response, headers, body]; | ||
verbs.forEach(function(verb) { | ||
_this.handlers[verb].push(handler); | ||
}); | ||
return _this; | ||
}, | ||
replyOnce: function replyOnce(code, response, headers) { | ||
var handler = [matcher, code, response, headers, body]; | ||
_this.replyOnceHandlers.push(handler); | ||
verbs.forEach(function(verb) { | ||
_this.handlers[verb].push(handler); | ||
}); | ||
return _this; | ||
} | ||
}; | ||
}; | ||
verbs.forEach(function(method) { | ||
VERBS.concat('any').forEach(function(method) { | ||
var methodName = 'on' + method.charAt(0).toUpperCase() + method.slice(1); | ||
MockAdapter.prototype[methodName] = function(matcher, body) { | ||
var _this = this; | ||
var matcher = matcher === undefined ? /.*/ : matcher; | ||
return { | ||
reply: function reply(code, response, headers) { | ||
var handler = [matcher, code, response, headers, body]; | ||
_this.handlers[method].push(handler); | ||
var handler = [matcher, body, code, response, headers]; | ||
addHandler(method, _this.handlers, handler); | ||
return _this; | ||
@@ -89,6 +69,12 @@ }, | ||
replyOnce: function replyOnce(code, response, headers) { | ||
var handler = [matcher, code, response, headers, body]; | ||
_this.handlers[method].push(handler); | ||
var handler = [matcher, body, code, response, headers]; | ||
addHandler(method, _this.handlers, handler); | ||
_this.replyOnceHandlers.push(handler); | ||
return _this; | ||
}, | ||
passThrough: function passThrough() { | ||
var handler = [matcher, body]; | ||
addHandler(method, _this.handlers, handler); | ||
return _this; | ||
} | ||
@@ -99,2 +85,12 @@ }; | ||
function addHandler(method, handlers, handler) { | ||
if (method === 'any') { | ||
VERBS.forEach(function(verb) { | ||
handlers[verb].push(handler); | ||
}); | ||
} else { | ||
handlers[method].push(handler); | ||
} | ||
} | ||
module.exports = MockAdapter; |
@@ -6,3 +6,3 @@ 'use strict'; | ||
function eql(a, b) { | ||
function isEqual(a, b) { | ||
return deepEqual(a, b, { strict: true }); | ||
@@ -25,5 +25,5 @@ } | ||
if (typeof handler[0] === 'string') { | ||
return url === handler[0] && isBodyMatching(body, handler[4]); | ||
return url === handler[0] && isBodyMatching(body, handler[1]); | ||
} else if (handler[0] instanceof RegExp) { | ||
return handler[0].test(url) && isBodyMatching(body, handler[4]); | ||
return handler[0].test(url) && isBodyMatching(body, handler[1]); | ||
} | ||
@@ -41,3 +41,3 @@ }); | ||
} catch (e) { } | ||
return parsedBody ? eql(parsedBody, requiredBody) : eql(body, requiredBody); | ||
return parsedBody ? isEqual(parsedBody, requiredBody) : isEqual(body, requiredBody); | ||
} | ||
@@ -97,2 +97,3 @@ | ||
module.exports = { | ||
find: find, | ||
findHandler: findHandler, | ||
@@ -99,0 +100,0 @@ purgeIfReplyOnce: purgeIfReplyOnce, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
33046
602
224
0
8