Permalink
Browse files

Dont send port if on standard port

  • Loading branch information...
1 parent 926ff84 commit 1b140c0b1eb315e33c124f41c3a0d9f6ca9479de @camshaft committed Feb 14, 2013
Showing with 64 additions and 9 deletions.
  1. +9 −7 index.js
  2. +1 −1 package.json
  3. +54 −1 test/base.test.js
View
16 index.js
@@ -1,14 +1,16 @@
module.exports = function() {
return function base(req, res, next) {
- var host = req.headers['x-forwarded-host']?
- req.headers['x-forwarded-host']+
- (req.headers['x-forwarded-port']?":"+req.headers['x-forwarded-port']:"")
- :req.headers.host,
- path = req.headers['x-forwarded-path'] || '',
- protocol = req.headers['x-forwarded-proto'] || req.protocol || "http";
+ var hostParts = req.headers.host.split(":")
+ , host = req.headers['x-forwarded-host'] || hostParts[0] || ""
+ , path = req.headers['x-forwarded-path'] || ""
+ , protocol = req.headers['x-forwarded-proto'] || req.protocol || "http"
+ , port = req.headers['x-forwarded-port'] || hostParts[1] || null;
- req.base = protocol+"://"+host+path;
+ if((port == 80 && protocol === "http") || (port == 443 && protocol === "https")) port = "";
+ if(port) port = ":"+port;
+
+ req.base = protocol+"://"+host+port+path;
next();
}
};
View
2 package.json
@@ -1,6 +1,6 @@
{
"name": "connect-base",
- "version": "0.0.1",
+ "version": "0.0.2",
"description": "Get the base url for Express/Connect",
"main": "index.js",
"directories": {
View
55 test/base.test.js
@@ -7,6 +7,14 @@ var app = connect();
app.use(base());
+app.use("/port", function(req, res) {
+ res.setHeader("content-type", "application/json");
+ res.end(JSON.stringify({
+ port: req.headers.host.split(":")[1],
+ base: req.base
+ }));
+});
+
app.use(function(req, res) {
res.end(req.base);
});
@@ -28,7 +36,8 @@ describe("Connect Base", function() {
request(app)
.get("/")
.set("X-Forwarded-Host", "example.com")
- .expect("http://example.com")
+ // Supertest binds to a random port
+ .expect(/http:\/\/example.com*/)
.end(function(err, res) {
if(err) done(err);
res.ok.should.be.ok;
@@ -78,4 +87,48 @@ describe("Connect Base", function() {
});
});
+ it("should not send the port if not specified", function(done) {
+ request(app)
+ .get("/port")
+ .set("X-Forwarded-Proto", "http")
+ .set("X-Forwarded-Host", "example.com")
+ .set("X-Forwarded-Path", "/testing")
+ .end(function(err, res) {
+ if(err) done(err);
+ res.ok.should.be.ok;
+ res.body.base.should.eql("http://example.com:"+res.body.port+"/testing");
+ done();
+ });
+ });
+
+ it("should not send the port on http and port 80", function(done) {
+ request(app)
+ .get("/")
+ .set("X-Forwarded-Proto", "http")
+ .set("X-Forwarded-Host", "example.com")
+ .set("X-Forwarded-Port", "80")
+ .set("X-Forwarded-Path", "/testing")
+ .expect("http://example.com/testing")
+ .end(function(err, res) {
+ if(err) done(err);
+ res.ok.should.be.ok;
+ done();
+ });
+ });
+
+ it("should not send the port on https and port 443", function(done) {
+ request(app)
+ .get("/")
+ .set("X-Forwarded-Proto", "https")
+ .set("X-Forwarded-Host", "example.com")
+ .set("X-Forwarded-Port", "443")
+ .set("X-Forwarded-Path", "/testing")
+ .expect("https://example.com/testing")
+ .end(function(err, res) {
+ if(err) done(err);
+ res.ok.should.be.ok;
+ done();
+ });
+ });
+
});

0 comments on commit 1b140c0

Please sign in to comment.