useFakeXMLHttpRequest never gives readyState == 4

183 views
Skip to first unread message

Seb

unread,
Jul 15, 2015, 12:17:45 PM7/15/15
to sin...@googlegroups.com
Hi All,

I've been puzzled for the whole day about this !

I'm trying to test Ajax using XMLHttpRequest (I'm not crazy, just debbuging why it is not working for REST.js) and readyState (in onreadystatechange) is never set to 4, and therefore it hangs until when it times out.

Any ideas ?

Thanks !

    beforeEach(function() {
      xhr
= sinon.useFakeXMLHttpRequest();
      requests
= [];
      xhr
.onCreate = function (req) { requests.push(req); };
   
});


    afterEach(function() {
      xhr
.restore();

   
});



      it('FAKE SERVER', function(done) {
       
var oReq = new XMLHttpRequest();
        oReq
.onreadystatechange = function() {
          console
.log('FAKE SERVER >> ' + oReq.readyState + " -> " + oReq.status + " -> " + JSON.stringify(oReq));
         
if (oReq.readyState==4) done();
       
}
       
try {
          oReq
.open('GET', '/api/testmodel/1');
          oReq
.send();
       
}
       
catch (ex) {
         
done();

          console
.log('FAKE SERVER >> ' + ex);
       
}


        console
.log('FAKE SERVER >> -- <<');
     
});

LOG: 'FAKE SERVER >> 1 -> 0 -> {"readyState":1,"requestHeaders":{},"requestBody":null,"status":0,"statusText":"","upload":{"eventListeners":{"progress":[],"load":[],"abort":[],"error":[]}},"responseType":"","response":"","withCredentials":false,"eventListeners":{"loadend":[null],"abort":[null],"load":[null],"loadstart":[null]},"method":"GET","url":"/api/testmodel/1","async":true,"responseText":null,"responseXML":null,"sendFlag":false}'
LOG
: 'FAKE SERVER >> 1 -> 0 -> {"readyState":1,"requestHeaders":{},"requestBody":null,"status":0,"statusText":"","upload":{"eventListeners":{"progress":[],"load":[],"abort":[],"error":[]}},"responseType":"","response":"","withCredentials":false,"eventListeners":{"loadend":[null],"abort":[null],"load":[null],"loadstart":[null]},"method":"GET","url":"/api/testmodel/1","async":true,"responseText":null,"responseXML":null,"sendFlag":true,"errorFlag":false}'
LOG
: 'FAKE SERVER >> -- <<'
     
FAKE SERVER

Denis Roarty

unread,
May 3, 2016, 11:28:43 AM5/3/16
to Sinon.JS, sebastian...@gmail.com
Was there never a response to this?   I am running into the same issue and don't see any resolutions online...  Looking at the fakeServer code, I don't see a way to set the readyState without editing Sinon's code.

Denis
Reply all
Reply to author
Forward
0 new messages