Skip to content

Commit

Permalink
Merge pull request #8 from dtryon/enhancement/support-timeouts
Browse files Browse the repository at this point in the history
enhancement - support timeout requests and stub timeouts
  • Loading branch information
mzabriskie committed Jan 10, 2017
2 parents 8efab10 + ed3aa7e commit 77990eb
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
52 changes: 50 additions & 2 deletions index.js
Expand Up @@ -4,6 +4,7 @@ import isURLSameOrigin from 'axios/lib/helpers/isURLSameOrigin'
import btoa from 'axios/lib/helpers/btoa'
import cookies from 'axios/lib/helpers/cookies'
import settle from 'axios/lib/core/settle'
import createError from 'axios/lib/core/createError'

const DEFAULT_WAIT_DELAY = 100

Expand All @@ -27,13 +28,34 @@ let mockAdapter = (config) => {
let stub = moxios.stubs.at(i)
if (stub.url === request.url ||
stub.url instanceof RegExp && stub.url.test(request.url)) {
request.respondWith(stub.response)
break
if (stub.timeout) {
throwTimeout(config)
}
request.respondWith(stub.response)
break
}
}
});
}

/**
* create common object for timeout response
*
* @param {object} config The config object to be used for the request
*/
let createTimeout = (config) => {
return createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED')
}

/**
* throw common error for timeout response
*
* @param {object} config The config object to be used for the request
*/
let throwTimeout = (config) => {
throw createTimeout(config)
}

class Tracker {
constructor() {
this.__items = []
Expand Down Expand Up @@ -131,6 +153,21 @@ class Request {
}
}

/**
* Respond to this request with a timeout result
*
* @return {Promise} A Promise that rejects with a timeout result
*/
respondWithTimeout() {
let response = new Response(this, createTimeout(this.config))
settle(this.resolve, this.reject, response)
return new Promise(function(resolve, reject) {
moxios.wait(function() {
reject(response)
})
})
}

/**
* Respond to this request with a specified result
*
Expand Down Expand Up @@ -162,6 +199,7 @@ class Response {
this.statusText = res.statusText
this.headers = res.headers
this.request = req
this.code = res.code
}
}

Expand Down Expand Up @@ -197,6 +235,16 @@ let moxios = {
this.stubs.track({url: urlOrRegExp, response})
},

/**
* Stub a timeout to be used to respond to a request matching a URL or RegExp
*
* @param {String|RegExp} urlOrRegExp A URL or RegExp to test against
*/
stubTimeout: function(urlOrRegExp) {
this.stubs.track({url: urlOrRegExp, timeout: true})
},

/**
/**
* Run a single test with mock adapter installed.
* This will install the mock adapter, execute the function provided,
Expand Down
29 changes: 29 additions & 0 deletions test.js
Expand Up @@ -99,6 +99,23 @@ describe('moxios', function () {
})
})

it('should timeout requests one time', function(done) {

moxios.uninstall()

moxios.withMock(function() {
axios.get('/users/12345')

moxios.wait(function() {
let request = moxios.requests.mostRecent()
request.respondWithTimeout().catch(function(err) {
equal(err.code, 'ECONNABORTED')
done()
})
})
})
})

it('should stub requests', function (done) {
moxios.stubRequest('/users/12345', {
status: 200,
Expand All @@ -114,6 +131,18 @@ describe('moxios', function () {
})
})

it('should stub timeout', function (done) {
moxios.stubTimeout('/users/12345')

axios.get('/users/12345').catch(onRejected)

moxios.wait(function () {
let err = onRejected.getCall(0).args[0]
deepEqual(err.code, 'ECONNABORTED')
done()
})
})

it('should stub requests RegExp', function (done) {
moxios.stubRequest(/\/users\/\d*/, {
status: 200,
Expand Down

0 comments on commit 77990eb

Please sign in to comment.