Skip to content

Commit 8c6d6a3

Browse files
committed
2 parents 3c8328f + aa3fbcf commit 8c6d6a3

File tree

4 files changed

+154
-3
lines changed

4 files changed

+154
-3
lines changed

lib/XMLHttpRequest.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,28 @@ exports.XMLHttpRequest = function() {
379379
self.dispatchEvent("readystatechange");
380380

381381
// Create the request
382-
request = doRequest(options, function(resp) {
382+
383+
function responseHandler(resp) {
384+
383385
response = resp;
386+
387+
if(response.statusCode === 302 || response.statusCode === 307 || response.statusCode === 303){
388+
settings.url = response.headers['location']
389+
var url = Url.parse(settings.url);
390+
host = url.hostname
391+
var newOptions = {
392+
hostname: url.hostname,
393+
port: url.port,
394+
path: url.path,
395+
method: response.statusCode === 303 ? 'GET' : settings.method,
396+
headers: headers
397+
}
398+
399+
request = doRequest(newOptions, responseHandler).on('error', errorHandler);
400+
request.end()
401+
return
402+
}
403+
384404
response.setEncoding("utf8");
385405

386406
setState(self.HEADERS_RECEIVED);
@@ -408,9 +428,12 @@ exports.XMLHttpRequest = function() {
408428
response.on('error', function(error) {
409429
self.handleError(error);
410430
});
411-
}).on('error', function(error) {
431+
}
432+
function errorHandler(error) {
412433
self.handleError(error);
413-
});
434+
}
435+
436+
request = doRequest(options, responseHandler).on('error', errorHandler);
414437

415438
// Node 0.4 and later won't accept empty data. Make sure it's needed.
416439
if (data) {

tests/test-redirect-302.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var sys = require("util")
2+
, assert = require("assert")
3+
, XMLHttpRequest = require("../lib/XMLHttpRequest").XMLHttpRequest
4+
, xhr = new XMLHttpRequest()
5+
, http = require("http");
6+
7+
// Test server
8+
var server = http.createServer(function (req, res) {
9+
10+
if(req.url === '/redirectingResource'){
11+
res.writeHead(302, {'Location': 'http://localhost:8000/'})
12+
res.end()
13+
return
14+
}
15+
16+
var body = "Hello World";
17+
res.writeHead(200, {
18+
"Content-Type": "text/plain",
19+
"Content-Length": Buffer.byteLength(body),
20+
"Date": "Thu, 30 Aug 2012 18:17:53 GMT",
21+
"Connection": "close"
22+
});
23+
res.write("Hello World");
24+
res.end();
25+
26+
this.close();
27+
}).listen(8000);
28+
29+
xhr.onreadystatechange = function() {
30+
if (this.readyState == 4) {
31+
assert.equal(xhr.getRequestHeader('Location'), '')
32+
assert.equal(xhr.responseText, "Hello World")
33+
sys.puts("done");
34+
}
35+
};
36+
37+
try {
38+
xhr.open("GET", "http://localhost:8000/redirectingResource");
39+
xhr.send();
40+
} catch(e) {
41+
console.log("ERROR: Exception raised", e);
42+
}

tests/test-redirect-303.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var sys = require("util")
2+
, assert = require("assert")
3+
, XMLHttpRequest = require("../lib/XMLHttpRequest").XMLHttpRequest
4+
, xhr = new XMLHttpRequest()
5+
, http = require("http");
6+
7+
// Test server
8+
var server = http.createServer(function (req, res) {
9+
10+
if(req.url === '/redirectingResource'){
11+
res.writeHead(303, {'Location': 'http://localhost:8000/'})
12+
res.end()
13+
return
14+
}
15+
16+
var body = "Hello World";
17+
res.writeHead(200, {
18+
"Content-Type": "text/plain",
19+
"Content-Length": Buffer.byteLength(body),
20+
"Date": "Thu, 30 Aug 2012 18:17:53 GMT",
21+
"Connection": "close"
22+
});
23+
res.write("Hello World");
24+
res.end();
25+
26+
this.close();
27+
}).listen(8000);
28+
29+
xhr.onreadystatechange = function() {
30+
if (this.readyState == 4) {
31+
assert.equal(xhr.getRequestHeader('Location'), '')
32+
assert.equal(xhr.responseText, "Hello World")
33+
sys.puts("done");
34+
}
35+
};
36+
37+
try {
38+
xhr.open("POST", "http://localhost:8000/redirectingResource");
39+
xhr.send();
40+
} catch(e) {
41+
console.log("ERROR: Exception raised", e);
42+
}

tests/test-redirect-307.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var sys = require("util")
2+
, assert = require("assert")
3+
, XMLHttpRequest = require("../lib/XMLHttpRequest").XMLHttpRequest
4+
, xhr = new XMLHttpRequest()
5+
, http = require("http");
6+
7+
// Test server
8+
var server = http.createServer(function (req, res) {
9+
10+
if(req.url === '/redirectingResource'){
11+
res.writeHead(307, {'Location': 'http://localhost:8000/'})
12+
res.end()
13+
return
14+
}
15+
16+
assert.equal(req.method, 'POST')
17+
18+
var body = "Hello World";
19+
res.writeHead(200, {
20+
"Content-Type": "text/plain",
21+
"Content-Length": Buffer.byteLength(body),
22+
"Date": "Thu, 30 Aug 2012 18:17:53 GMT",
23+
"Connection": "close"
24+
});
25+
res.write("Hello World");
26+
res.end();
27+
28+
this.close();
29+
}).listen(8000);
30+
31+
xhr.onreadystatechange = function() {
32+
if (this.readyState == 4) {
33+
assert.equal(xhr.getRequestHeader('Location'), '')
34+
assert.equal(xhr.responseText, "Hello World")
35+
sys.puts("done");
36+
}
37+
};
38+
39+
try {
40+
xhr.open("POST", "http://localhost:8000/redirectingResource");
41+
xhr.send();
42+
} catch(e) {
43+
console.log("ERROR: Exception raised", e);
44+
}

0 commit comments

Comments
 (0)