Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[feature] added ResponseStream.prototype.redirect() and related tests

  • Loading branch information...
commit 83814d97228abfd9cf56323e85e94b24076347fe 1 parent 942cc6d
@yawnt yawnt authored
View
BIN  examples/simple/.simple.js.swp
Binary file not shown
View
10 examples/simple/simple.js
@@ -19,7 +19,7 @@ var server = union.createServer({
});
router.get('/foo', function () {
- this.res.writeHead(200, { 'Content-Type': 'text/plain' })
+ this.res.writeHead(200, { 'Content-Type': 'text/plain' });
this.res.end('hello world\n');
});
@@ -37,6 +37,14 @@ router.post('/foo', { stream: true }, function () {
});
});
+router.get('/redirect', function() {
+ this.res.redirect('http://www.google.com');
+});
+
+router.get('/custom_redirect', function() {
+ this.res.redirect('/foo', 301);
+});
+
server.listen(9090);
console.log('union with director running on 9090');
View
BIN  lib/.response-stream.js.swp
Binary file not shown
View
11 lib/response-stream.js
@@ -159,3 +159,14 @@ ResponseStream.prototype.write = function (data) {
this.emit('data', data);
}
};
+
+ResponseStream.prototype.redirect = function(path, status) {
+ path = path.indexOf('://') ?
+ path :
+ (this.req.encrypted ? 'https://' : 'http://') + this.req.headers.host + this.req.url;
+
+ this.res.writeHead(status || 302, {
+ 'Location': path
+ });
+ this.res.end();
+};
View
BIN  test/.simple-test.js.swp
Binary file not shown
View
24 test/simple-test.js
@@ -48,6 +48,30 @@ vows.describe('union/simple').addBatch({
macros.assertValidResponse(err, res);
assert.equal(body, 'wrote to a stream!');
}
+ },
+ "a GET request to `/redirect`": {
+ topic: function() {
+ request.get({
+ url: 'http://localhost:9090/redirect',
+ followRedirect: false
+ }, this.callback);
+ },
+ "it should redirect to `http://www.google.com`": function(err, res, body) {
+ assert.equal(res.statusCode, 302);
+ assert.equal(res.headers.location, "http://www.google.com");
+ }
+ },
+ "a GET request to `/custom_redirect`": {
+ topic: function() {
+ request.get({
+ url: 'http://localhost:9090/custom_redirect',
+ followRedirect: false
+ }, this.callback);
+ },
+ "it should redirect to `/foo`": function(err, res, body) {
+ assert.equal(res.statusCode, 301);
+ assert.equal(res.headers.location, "/foo");
+ }
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.