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

Implement passThrough and reply() with Promise #21

Merged
merged 2 commits into from
Sep 23, 2016
Merged

Implement passThrough and reply() with Promise #21

merged 2 commits into from
Sep 23, 2016

Conversation

villesinisalo
Copy link
Contributor

Resolves #19 and #20:

  • Use passThrough: true to route non-mocked endpoints back to Axios
  • Add support for .reply(function) itself returning a Promise

I put a note in README.md that support for replying with a Promise will come out in version 1.7.0. Please edit if next version is not that.

@ctimmerm
Copy link
Owner

Thanks a lot for the PR, this is great!

@ctimmerm ctimmerm merged commit b3d3532 into ctimmerm:master Sep 23, 2016
@ctimmerm
Copy link
Owner

Before tagging a new release, I was thinking what the best API for this would be.
Concerning what to do when no matching handler is found, there are currently the following scenarios:

  1. The default behavior: an unmatched request will resolve with a 404 response

  2. You want to return a different status code/message:

    // Have this as the last handler registered
    mock.onAny(/.*/).reply(500, "No matched handler found");
  3. Passing through requests to the real backend:

    var mock = new MockAdapter(axios, { passThrough: true });

which means the same behavior is modified in different ways.
An additional problem with the passThrough configuration option is that it's ambiguous in the context where it's defined; it's not obvious only request that do not match a handler are passed through.

My suggestion is to instead of having a configuration option on the instance, configure it through a handler instead:

mock.onAny(/.*/).passThrough();

It could be even nicer if the API is changed to match all urls if no url is given:

mock.onAny().passThrough();

One extra advantage of this approach is that it also enables selectively passing through requests to the backend:

// Only pass through GET requests
mock.onGet().passThrough();

// Only pass through requests that start with "/api"
mock.onAny(/^\/api/).passThrough();

What are your thoughts?

@villesinisalo
Copy link
Contributor Author

There are indeed now several ways to configure the same functionality. I was actually originally thinking of just adding support for returning a Promise from .reply(), since it alone provides (yet another) way to achieve this:

let normalAxios = axios.create();
mock.onGet(/^\/api/).reply(config => normalAxios.request(config));

But then again, that is somewhat cumbersome (need to create a separate Axios instance, and easy to accidentally create an infinite loop if you forward the request to the same Axios instance where you have the mock adapter!)

I can change it to support the functionality with the .passThrough() method. For my usecase they are both equally good, but you're right your approach allows for better customisability. It also "reads" better: "mock: on get /foo: pass through"

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

Successfully merging this pull request may close these issues.

None yet

2 participants