Skip to content
10 changes: 10 additions & 0 deletions lib/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ function enableXHROverH2(XMLHttpRequest, configuration) {
throw new SyntaxError("Synchronous is not supported");
}

// Reset default state
if (this.readyState !== 0) {
redefine(this, 'readyState', XMLHttpRequest.UNSENT);
xhrInfo.put(this, 'lastreadystate', XMLHttpRequest.UNSENT);
defineXhrResponse(this, { data: '' });
}

xhrInfo.put(this, 'method', method);
xhrInfo.put(this, 'url', url);
xhrInfo.put(this, 'async', async);
Expand All @@ -114,6 +121,9 @@ function enableXHROverH2(XMLHttpRequest, configuration) {

var self = this;

delete self.response;
delete self.responseText;

/*
* We need to fire opened event here but native library might
* recall open, so we do this
Expand Down
5 changes: 3 additions & 2 deletions test/http2-push-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,12 +585,14 @@ describe('http2-push', function () {
];
var requestCount = 0;
socketOnRequest = function (request, response) {
var pr;

requestCount++;
if (requestCount === 1) {
// TODO check request headers and requests responses
assert.equal(request.url, '/stream');

var pr = response.push({
pr = response.push({
'path': '/cachedGetRequestAfterFailure',
'protocol': 'http:'
});
Expand Down Expand Up @@ -619,7 +621,6 @@ describe('http2-push', function () {
// TODO check request headers and requests responses
assert.equal(request.url, '/cachedGetRequestAfterFailure');


var pr2 = response.push({
'path': '/cachedGetRequestAfterFailure',
'protocol': 'http:'
Expand Down
81 changes: 80 additions & 1 deletion test/http2-xhr-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe('http2-xhr', function () {
response.write(message);
response.end();
};

XMLHttpRequest.proxy(["http://localhost:7080/config"]);
var xhr = new XMLHttpRequest();

Expand All @@ -98,8 +99,8 @@ describe('http2-xhr', function () {
done();
}
};
xhr.open('GET', 'http://cache-endpoint2/path', true);

xhr.open('GET', 'http://cache-endpoint2/path', true);
xhr.send(null);

});
Expand Down Expand Up @@ -647,6 +648,84 @@ describe('http2-xhr', function () {
firstRequest.send(null);
});

it('should allow reuse of XMLHttpRequest for different url', function (done) {
var messages = [
"Hello, Dave. You're looking well today.",
"Do you want to be my friend, Dave ?"
];
var requestCount = 0;
socketOnRequest = function (request, response) {
requestCount++;
if (requestCount === 1) {
// TODO check request headers and requests responses
assert.equal(request.url, '/reuseXMLHttpRequest1');
response.setHeader('Content-Type', 'text/html');
response.setHeader('Content-Length', messages[0].length);
response.setHeader('Cache-Control', 'private, max-age=5');
response.write(messages[0]);
response.end();
} else if (requestCount === 2) {
// TODO check request headers and requests responses
assert.equal(request.url, '/reuseXMLHttpRequest2');
response.setHeader('Content-Type', 'text/html');
response.setHeader('Content-Length', messages[1].length);
response.setHeader('Cache-Control', 'private, max-age=5');
response.write(messages[1]);
response.end();
} else {
throw new Error("Should only get 2 request");
}
};
XMLHttpRequest.proxy(["http://localhost:7080/config"]);
var firstRequest = new XMLHttpRequest();

var statechanges = 0;
firstRequest.onreadystatechange = function () {
++statechanges;
if(statechanges !== 1) {
assert.equal(statechanges, firstRequest.readyState);
}
if (firstRequest.readyState >= 2) {
assert.equal(firstRequest.status, 200);
assert.equal(firstRequest.statusText, "OK");
}
if (firstRequest.readyState >= 3) {
assert.equal(firstRequest.response, messages[0]);
}
if (firstRequest.readyState === 4 && firstRequest.status === 200) {
assert.equal(firstRequest.response, messages[0]);

var secondRequest = firstRequest;
var statechangesocket = 0;
secondRequest.onreadystatechange = function () {
++statechangesocket;
// TODO !==1 is due to bug
if(statechangesocket !== 1) {
assert.equal(statechangesocket, secondRequest.readyState);
}
if (secondRequest.readyState >= 2) {
assert.equal(secondRequest.status, 200);
assert.equal(secondRequest.statusText, "OK");
}
if (secondRequest.readyState >= 3) {
assert.equal(secondRequest.response, messages[1]);
}
if (secondRequest.readyState === 4 && secondRequest.status === 200) {
assert.equal(secondRequest.response, messages[1]);
done();
}
};

secondRequest.open('GET', 'http://cache-endpoint2:80/reuseXMLHttpRequest2', true);
secondRequest.send(null);
}
};

firstRequest.open('GET', 'http://cache-endpoint2:80/reuseXMLHttpRequest1', true);

firstRequest.send(null);
});

it('should not cache GET request and not reuse if error', function (done) {
var messages = [
"Hello, Dave. You're looking well today.",
Expand Down