Closed davidjohnbell closed 4 years ago
We are trying to keep the GitHub issues list tidy and focused on bugs and feature discussions. This ticket looks like a usage question; please post it to StackOverflow and tag it with sinon
, so the bigger community can help answer your questions.
If you feel that your topic is an issue with Sinon, please open a new ticket and follow the guidelines for reporting an issue.
I think there is a clear lack of documentation.
If xhr.error()
is used, one could thing of an HTTP error, but the fact is that the client xhr object is changed in such a way that any further request to that server
will trigger this error.
This is unexpected and most people testing a timeout followed by a connection success (and using .error()
to simulate the timeout) will encounter this.
A workaround is to use server.respond([400, {}, 'Error']);
which does not "corrupt" the state of the xhr client. But its resulting state may not actually mimic a real network error.
@drzraf You are probably right that we could need some improved docs! If you feel like getting it done, we do accept pull requests 👍 The docs are automatically generated from the repo.
In this case, I think it's quite clear what this does from the docs.
request.error(); Simulates a network error on the request. The onerror handler will be called and the status will be 0.
A network error is just that: an error on the network level. If you return a HTTP status code, that implies the network is working, so that would not make much sense. Do feel free to elaborate on the doc examples, though!
Yes, but the problem I (and possibly OP) experience, is that we can't use the fake-server afterwards. Sending a new XMLHttpRequest
will (likely?) provoke an INVALID_STATE_ERR
failure (0
in my case). This is not documented, I ignore the reason of this behavior and whether it's a bug or not.
Ah, good point! I did not get that it made the server unusable for the following requests. That both needs documenting and/or potentially a fix. Would you care to create a feature request describing the manner in which you think this should work? Perhaps with an example use case that does not work today.
I want to simulate a temporary network failure to see how my code react to it (For example, it should retry X times).
In case of xhr.error()
, my code handles the state of the XMLHttpRequest and will trigger new ones.
Again, I can't tell in which internal state nise's XHR server must be left but I believe it should be capable of accepting new requests.
Im writing a chrome extension that is essentially a proxy. I'm getting an invalid state error when trying to respond to a fake xhr request. When the
proxyRequest
method executes I'm seeing in the console thatonload
is triggered before I callrespond
. Thank you in advance for any help.