Skip to content

WIP: Omit certain paths from mocking #29

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft

Conversation

lukaszfiszer
Copy link
Collaborator

@lukaszfiszer lukaszfiszer commented May 10, 2021

This PR will introduce mockiavelli.passThrough method, that gives user the ability to define which endpoints should not be mocked (requested in #10 ):

mockiavelli.passThrough('/do/not/mock');

Any request that matches the URL will be forwarded to the original endpoint using request.continue() / route.continue() methods from ppter/playwright APIs.

It will work very similar to existing mockiavelli.mock methods and use the same underlying request matching mechanism. User will be able to mix and override mockiavelli.passThrough with mockiavelli.mock

// do not mock any endpoint under /foo 
mockiavelli.passThrough('/foo/**/*'); 

// mock particular /foo/bar endpoint
mockiavelli.mockGET('/foo/bar', {status: 200, body: []});

Few internal improvements are required in order to make this method more user-friendly:

@Fiszcz
Copy link
Collaborator

Fiszcz commented May 11, 2021

The idea with passThourght looks OK for me.

The solution with passThrough is very similar to the passthrough from the MirageJs package.
https://miragejs.com/api/classes/server/#passthrough

Additionally, there is a option to mock specific http method given as second parameter: this.passthrough('/endpoint', ['GET']).
And there is also an option to pass own function which will decide to skip or pass request:

this.passthrough(request => {
  return request.queryParams.skipMirage;
});

There is also very interesting pattern in the MSW package: https://mswjs.io/docs/api/setup-worker/start#onunhandledrequest
where we can decide what should be done with unhandled requests (bypass, print warning, or throw error). We could add this option for Mockiavelli instance object.

So, what do you think about the above ideas.

@lukaszfiszer
Copy link
Collaborator Author

lukaszfiszer commented May 11, 2021

The idea with passThourght looks OK for me.

The solution with passThrough is very similar to the passthrough from the MirageJs package.
https://miragejs.com/api/classes/server/#passthrough

Good. Seems we're going in the good direction then

Additionally, there is a option to mock specific http method given as second parameter: this.passthrough('/endpoint', ['GET']).

This would also be possible with .passThrough({method: 'GET', url: '/endpoint'}). The first argument will be handled in the same way as in .mock():.

And there is also an option to pass own function which will decide to skip or pass request:

this.passthrough(request => {
  return request.queryParams.skipMirage;
});

Not sure what "skipping" in this context means. In our case, a request must either be responded by mockiavelli, or passed through to the original server.

There is also very interesting pattern in the MSW package: https://mswjs.io/docs/api/setup-worker/start#onunhandledrequest
where we can decide what should be done with unhandled requests (bypass, print warning, or throw error). We could add this option for Mockiavelli instance object.

Yes, I had a similar idea as well. However, the case of Mockiavelli is a bit different. It intercepts all types of requests, and treats unhandled XHR/fetch requests differently (error) than other requests (pass-through). So a simple "passtrough" / "error" option does not fit in here.

Allow to mock HTTP request regardless of request method.
@lukaszfiszer
Copy link
Collaborator Author

reopening - this shouldn't be closed automatically

@lukaszfiszer lukaszfiszer reopened this May 11, 2021
@tavoli
Copy link

tavoli commented Jun 28, 2021

I really need it

@Fiszcz
Copy link
Collaborator

Fiszcz commented Oct 29, 2021

@lukaszfiszer what's up with it?

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.

3 participants