axios-mock-adapter icon indicating copy to clipboard operation
axios-mock-adapter copied to clipboard

Missleading regex example

Open Sshnyari opened this issue 7 years ago • 5 comments

When using a regex expression, requests don't get caught by the adapter in some cases

This works :

const axios = axios.create({
  baseURL: 'https://apiurl.com'
})
axios.get('/foo/123').then(function (data) {
  console.log(data)
})

mock.onGet(/\/foo\/\d+/).reply(function(config) {
  return [200, {}];
})

This doesn't :

const axios = axios.create({
  baseURL: 'https://apiurl.com/'
})
axios.get('/foo/123').then(function (data) {
  console.log(data)
})

mock.onGet(/\/foo\/\d+/).reply(function(config) {
  return [200, {}];
})

It is due to the trailing slash in the baseURL.

Is there a way to fix that (in handleRequest function for example) or at least change the documentation ?

Sshnyari avatar May 17 '17 20:05 Sshnyari

got into this bug, but @Sshnyari workaround not worked for me for some reason here i got false and

typeof handler[0]

is object, but

} else if (handler[0] instanceof RegExp) {

returns false i'm still do not know why instanceof RegExp not working, but if I replace condition with

} else if ({}.toString.call(handler[0])==='[object RegExp]') {

everything works

any thoughts? node v8.1.4

wateryoma avatar Jul 20 '17 22:07 wateryoma

@wateryoma I don't think your problem is related to this issue. Can you post a complete code example so that it's easier to see what's going on?

ctimmerm avatar Jul 21 '17 19:07 ctimmerm

sure, here is mock regexp mock not working, but with "fix" i wrote above it's working

i use webpack-everything as a starter in this project, just clone repo, install dependencies and run

yarn && yarn dev

open localhost:3000 and navigate to localhost:3000/test then watch console i doubt that this is issue with axios-mock-adapter, but it's definitely a strange one

wateryoma avatar Jul 21 '17 20:07 wateryoma

2020-08-17 The problem remains unsolved

zzx18023 avatar Aug 17 '20 03:08 zzx18023

Backing up this issue. Could regex not be working at all ?

I do

const res = await this.$axios.$get('/api/foobar/123')

⭕ This works (with url as string): 200

  mock.onGet('/api/foobar/123').reply(function (options) {
    return [200, { msg: 'hello' }]
  });

❌ This does not (with url as regex) : 404

  mock.onGet(/\/api\/foobar\/123/).reply(function (options) {
    return [200, { msg: 'hello' }]
  });

And i haven't introduce a \d+ in the regex yet.

I tried with both of these base url :

axios: {
  baseURL: 'http://localhost:3000/' // either one
  baseURL: 'http://localhost:3000'  // same result
}

Looks like regex they're just actually not supported or not working at the time of writing this. Or am i dumb af ?

CaptainFalcon92 avatar Nov 03 '21 12:11 CaptainFalcon92