Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow function matcher #125

Open
flaviojs opened this issue Mar 19, 2018 · 0 comments
Open

Allow function matcher #125

flaviojs opened this issue Mar 19, 2018 · 0 comments

Comments

@flaviojs
Copy link

flaviojs commented Mar 19, 2018

I have a GET request with the url in the following format:
api/v1/rest/collection?startDate>="(ISO 8601 string)"&startDate<"(ISO 8601 string)"

I want to let it pass through (to a default baseURL) when the startDate range is outside the specific range I am mocking.

The simplest way to enable this is by allowing a matcher function as an argument to onXXX methods.
To support other complex mocking scenarios it could receive the (mutable) config object that the reply function also receives.

This is how I would use it:

var axios = require('axios')
var AxiosMockAdapter = require('axios-mock-adapter')

axios.default.defaults.baseURL = 'http://url.to.test.api'
var mock = new AxiosMockAdapter(axios, {
  delayResponse: 100 // ms
})

var data = [] // mocked data ordered by startDate
mock.onGet(function (config) {
  var start // parse "startDate>=..." from query of config.url
  var end // parse "startDate<..." from query of config.url
  if (config.url.indexOf('api/v1/rest/collection?') === 0 && data[0].startDate <= start && data[data.length-1].startDate >= end) {
    config.start = start
    config.end = end
    return true
  }
  return false
}).reply(function (config) {
  console.log('mock ' + config.url)
  return [200, data.filter(x => x.startDate >= config.start && x.startDate < config.end)]
})

mock.onAny().passThrough()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant