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

onSend not documented #26

Open
refractalize opened this issue Aug 17, 2016 · 0 comments
Open

onSend not documented #26

refractalize opened this issue Aug 17, 2016 · 0 comments

Comments

@refractalize
Copy link

refractalize commented Aug 17, 2016

I want responses to be dependent on the request, so I was looking for a way to be called when a request was made, so that I could examine the request and make the appropriate response, like any normal server-side would.

I found onSend, on this line here: https://github.com/pretenderjs/FakeXMLHttpRequest/blob/master/src/fake-xml-http-request.js#L281

Which serves the purpose quite well, however I'm forced to use it like this:

var fakeXhr = require('fake-xml-http-request');

fakeXhr.prototype.onSend = function (xhr) {
  xhr.respond(200, {'Content-Type': 'text/plain'}, 'you requested ' + xhr.method + ' ' + xhr.url);
};

The bit I'm not sure about is, simply from an API point of view, that we should be overriding a method on the prototype. Shouldn't there be a more formal (and documented) API for responding to requests like this?

I'm thinking something like, perhaps: fakeXhr.on('request', function (xhr) { ... }), and fakeXhr.off('request', ...). Or fakeXhr.onrequest = function (xhr) { ... }.

Happy to make a PR if you have a preference.

By the way, I'm planning to use this in mock-xhr-router.

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

No branches or pull requests

1 participant