mockiavelli icon indicating copy to clipboard operation
mockiavelli copied to clipboard

WIP: Omit certain paths from mocking

Open lukaszfiszer opened this issue 3 years ago • 5 comments

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:

  • [x] support for matching request of any method when only URL is provided - #30
  • [x] support for wildcard parameters /api/**/*, **/*.js - #31

lukaszfiszer avatar May 10 '21 12:05 lukaszfiszer

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.

Fiszcz avatar May 11 '21 06:05 Fiszcz

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.

lukaszfiszer avatar May 11 '21 10:05 lukaszfiszer

reopening - this shouldn't be closed automatically

lukaszfiszer avatar May 11 '21 14:05 lukaszfiszer

I really need it

tavoli avatar Jun 28 '21 18:06 tavoli

@lukaszfiszer what's up with it?

Fiszcz avatar Oct 29 '21 10:10 Fiszcz