jameslnewell / xhr-mock

Utility for mocking XMLHttpRequest.
196 stars 48 forks source link

Support for the file protocol #81

Open radarfox opened 5 years ago

radarfox commented 5 years ago

Hello,

awesome project, helped a lot. Thanks! Only problem I found is that mocking URLs starting with file:// isn't working properly.

This doesn't work:

xhrMock.setup();
const url = 'file:///C:/web/index.html';
xhrMock.get(url , {body: '<html></html>'});
const xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.send();

It outputs this error:

ERROR: 'xhr-mock: No handler returned a response for the request.
GET file:/C:/web/index.html HTTP/1.1

So my current hotfix is this:

xhrMock.setup();
xhrMock.get('file:/C:/web/index.html', {body: '<html></html>'});
const xhr = new XMLHttpRequest();
xhr.open('GET', 'file:///C:/web/index.html', true);
xhr.send();