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

`mock.onGet()` matches URL with trailing slash

Open souldzin opened this issue 7 years ago • 4 comments

What is the current behavior?

In the code below, the "mock.onGet without trailing slash" test fails.

import axios from 'axios';
import MockAdapter from 'axios-mock-adapter'

class Foo {
  constructor(endpoint) {
    this.axios = axios.create({
      baseURL: endpoint
    });
  }

  getFoos() {
    return this.axios.get();
  }

  getFooBars() {
    return this.axios.get("bar");
  }
}

describe("Foo", () => {
  const ENDPOINT = "http://test.com/bogus"
  let mock;
  let subject;

  beforeEach(() => {
      mock = new MockAdapter(axios);
      subject = new Foo(ENDPOINT);
  })

  afterEach(() => {
    mock.restore();
  });
  
  // ----- This test fails :( ---------------------
  it("mock.onGet without trailing slash", () => {
    mock.onGet(ENDPOINT).reply(200, {});

    return subject.getFoos();
  });  

  // ----- This one passes O_o --------------------
  it("mock.onGet with trailing slash", () => {
    mock.onGet(`${ENDPOINT}/`).reply(200, {});

    return subject.getFoos();
  })
});

What is expected?

  • I expect the "mock.onGet without trailing slash" test to pass.
  • I'm not sure if the "mock.onGet with trailing slash" should pass or not... I would think not...

What is causing this?

The current implementation for combineUrls() leaves a trailing slash when url is empty. (See utils.js for more info).

I would expect this to be consistent with the way axios combines URLs. Maybe we can even import this method?

What am I willing to do about it?

I can create a PR that updates the implementation of combineUrls(), if it is agreed that this is an issue.

souldzin avatar May 09 '18 18:05 souldzin

I am having a similar issue. For me even adding a trailing slash does not work. However if I put the url as regular expresion with /* at the end it works.

javierguzman avatar Oct 12 '19 11:10 javierguzman

I think this should be closed since we can achieve the result by making the a regex .*

babacarcissedia avatar Nov 01 '20 14:11 babacarcissedia

@bcdbuddy I am not a maintainer but in my opinion because we can do it with regex does not mean is enough for closing the issue. I mean, it should work without regex, shouldn't it?

javierguzman avatar Nov 01 '20 15:11 javierguzman

I don't think so. I am assuming it is only doing string comparison so if there is a trailing slash or not that can cause a mismatch. Simple enough. As long as there is one way of achieving the expected result without any change on this lib I think we should go for that option. In any case, this repo seems dead

babacarcissedia avatar Nov 01 '20 23:11 babacarcissedia