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

Missleading regex example #54

Open
Sshnyari opened this issue May 17, 2017 · 5 comments
Open

Missleading regex example #54

Sshnyari opened this issue May 17, 2017 · 5 comments

Comments

@Sshnyari
Copy link

Sshnyari commented May 17, 2017

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 ?

@ctimmerm ctimmerm added the bug label Jun 9, 2017
@wateryoma
Copy link

wateryoma commented Jul 20, 2017

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

@ctimmerm
Copy link
Owner

@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?

@wateryoma
Copy link

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

@ctimmerm ctimmerm added the test label Jan 2, 2019
@zzx18023
Copy link

2020-08-17 The problem remains unsolved

@CaptainFalcon92
Copy link

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 ?

@jamyouss jamyouss mentioned this issue Oct 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants