Skip to content

Commit aa3fbcf

Browse files
committed
added support for status code 303, tests for 302, 303, 307
1 parent 3fb6fc9 commit aa3fbcf

File tree

4 files changed

+130
-2
lines changed

4 files changed

+130
-2
lines changed

lib/XMLHttpRequest.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,15 +383,15 @@ exports.XMLHttpRequest = function() {
383383

384384
response = resp;
385385

386-
if(response.statusCode === 302){
386+
if(response.statusCode === 302 || response.statusCode === 307 || response.statusCode === 303){
387387
settings.url = response.headers['location']
388388
var url = Url.parse(settings.url);
389389
host = url.hostname
390390
var newOptions = {
391391
hostname: url.hostname,
392392
port: url.port,
393393
path: url.path,
394-
method: settings.method,
394+
method: response.statusCode === 303 ? 'GET' : settings.method,
395395
headers: headers
396396
}
397397

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)