marmelab / FakeRest

Patch fetch/XMLHttpRequest to fake a REST API server in the browser, based on JSON data.
MIT License
439 stars 48 forks source link

Whitelist URLs to not intercept? #27

Closed daumann closed 7 years ago

daumann commented 7 years ago

I have set up fakerest with fetchMock.mock('^http://fakeapi', restServer.getHandler())

Now when I want to test my auth api, I have a request going out fetch(request) with request being: Request {method: "POST", url: "http://localhost:4040/v1/auth/login", ...

Unfortunately the request does not go out but gets intercepted and results in the error:

Uncaught Error: No fallback response defined for GET to [object Request]
    at FetchMock.webpackJsonp../node_modules/fetch-mock/es5/fetch-mock.js.FetchMock.fetchMock (fetch-mock.js:104)
    at eval (eval at webpackJsonp../src/components/menu/authentication/authClient.js.__webpack_exports__.a (authClient.js:13), <anonymous>:1:1)
    at webpackJsonp../src/components/menu/authentication/authClient.js.__webpack_exports__.a (authClient.js:13)
    at runCallEffect (proc.js:503)
    at runEffect (proc.js:425)
    at next (proc.js:306)
    at currCb (proc.js:378)
    at proc.js:489
    at exec (scheduler.js:19)
    at flush (scheduler.js:60)

Is there a way to let requests to a specific host through?

fzaninotto commented 7 years ago

What if you loose the ^?

daumann commented 7 years ago

@fzaninotto if I change it to fetchMock.mock('http://fakeapi', restServer.getHandler()) all requests (even those ment for http://fakeapi) throw that Uncaught Error above.

fzaninotto commented 7 years ago

And how did you initialize your restServer?

daumann commented 7 years ago

@fzaninotto const restServer = new FakeRest.FetchServer('http://fakeapi');

fzaninotto commented 7 years ago

Then I suspect a bug in the fetch-mock package, which is a dependency...

daumann commented 7 years ago

Adding a .spy(): fetchMock.mock('^http://fakeapi', restServer.getHandler()).spy() did the trick. This lets other URLs through. Thanks for sending me to fetch-mock, that's where I found it.