Skip to content
New issue

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

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

Already on GitHub? # to your account

Unable to call same endpoint twice with different responses #68

Closed
sslotsky opened this issue Aug 7, 2017 · 2 comments
Closed

Unable to call same endpoint twice with different responses #68

sslotsky opened this issue Aug 7, 2017 · 2 comments

Comments

@sslotsky
Copy link

sslotsky commented Aug 7, 2017

Testing some re-authentication logic that retries a request after refreshing. Snippet should be fairly explanatory:

    context('when auth token has expired', () => {
      const code = 'auth_token_expired';
      const refresh = { refresh_token: 'refresh', user: {} };
      const user = { name: 'bob' };
      const success = { results: [], totalCount: 0 };

      context('when refresh succeeds', () => {
        beforeEach(() => {
          spyOn(session, 'refreshToken').andReturn('refresh');

          mockAdapter.onGet(endpoint).replyOnce(401, { code })
            .onGet(endpoint).replyOnce(200, success)
            .onPost('/sessions/refresh').reply(200, refresh);
        });

        it('retries the original request', (done) => {
          adapter.get(endpoint).then((resp) => {
            expect(response).toEqual(success);
          }).catch(e => {
            debugger
          });
        });

The first call to endpoint gives me a 401, but the second one gives me a 404. I suspect it's due to the way I'm executing the second call:

adapter.interceptors.response.use(undefined, (error) => {
  if (error.response.status === 401) {
    if (error.response.data.code === 'auth_token_expired') {
      const refresh = session.refreshToken();

      if (refresh) {
        return adapter.post('/sessions/refresh', { refresh_token: refresh }).then((resp) => {
          session.login(resp.data);
          return adapter(error.config);
        }).catch((e) => {

When I call adapter(error.config), the effect is that the endpoint is called again, but I don't think axios-mock-adapter recognizes it as being the same call. Is this a bug, or expected? Is there a way I can work around it?

@sslotsky
Copy link
Author

sslotsky commented Aug 7, 2017

In fact, it even raises a 404 when I use onAny

          mockAdapter.onGet(endpoint).replyOnce(401, { code })
            .onPost('/sessions/refresh').reply(200, refresh)
            .onAny().replyOnce(200, success);

@ammmze
Copy link
Contributor

ammmze commented Feb 26, 2018

@sslotsky i've got a fix for this issue in PR #119. As a workaround, you can do the following in your interceptor before you call return adapter(error.config):

if (error.config.adapter === null) {
    delete error.config.adapter;
}

@ctimmerm ctimmerm closed this as completed Jan 2, 2019
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants