Permalink
Browse files

Initial commit

  • Loading branch information...
1 parent 95cf090 commit f7ab0063fe5c8361e4683bcbba8cc1043445b09f @camshaft committed Jan 15, 2013
Showing with 157 additions and 1 deletion.
  1. +1 −0 .gitignore
  2. +4 −0 .travis.yml
  3. +32 −1 README.md
  4. +14 −0 index.js
  5. +25 −0 package.json
  6. +81 −0 test/base.test.js
View
@@ -12,3 +12,4 @@ logs
results
npm-debug.log
+node_modules
View
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+ - 0.6
+ - 0.8
View
@@ -1,4 +1,35 @@
connect-base
============
-Get the base url for Express/Connect
+Get the base url for Express/Connect
+
+```js
+var app = connect();
+ base = require("connect-base");
+
+app.use(base());
+
+app.use(function(req, res){
+ res.send(req.base);
+})
+```
+
+```sh
+curl http://localhost:5000
+> http://localhost:5000
+```
+
+```sh
+curl http://example.com
+> http://example.com
+```
+
+```sh
+curl -H "X-Forwarded-Host: test.example.com" http://example.com
+> http://test.example.com
+```
+
+```sh
+curl -H "X-Forwarded-Host: test.example.com" -H "X-Forwarded-Path: /testing" http://example.com
+> http://test.example.com/testing
+```
View
@@ -0,0 +1,14 @@
+
+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";
+
+ req.base = protocol+"://"+host+path;
+ next();
+ }
+};
View
@@ -0,0 +1,25 @@
+{
+ "name": "connect-base",
+ "version": "0.0.0",
+ "description": "Get the base url for Express/Connect",
+ "main": "index.js",
+ "directories": {
+ "test": "test"
+ },
+ "scripts": {
+ "test": "NODE_ENV=test node_modules/.bin/mocha -R spec"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/CamShaft/connect-base.git"
+ },
+ "author": "Cameron Bytheway <cameron@nujii.com>",
+ "license": "MIT",
+ "readmeFilename": "README.md",
+ "devDependencies": {
+ "should": "~1.2.1",
+ "mocha": "~1.8.1",
+ "connect": "~2.7.2",
+ "supertest": "~0.5.1"
+ }
+}
View
@@ -0,0 +1,81 @@
+var should = require("should"),
+ connect = require("connect"),
+ request = require("supertest"),
+ base = require("..");
+
+var app = connect();
+
+app.use(base());
+
+app.use(function(req, res) {
+ res.end(req.base);
+});
+
+describe("Connect Base", function() {
+
+ it("should return the base url", function(done) {
+ request(app)
+ .get("/")
+ .expect(/http:\/\/127.0.0.1:/)
+ .end(function(err, res) {
+ if(err) done(err);
+ res.ok.should.be.ok;
+ done();
+ });
+ });
+
+ it("should return the forwarded host", function(done) {
+ request(app)
+ .get("/")
+ .set("X-Forwarded-Host", "example.com")
+ .expect("http://example.com")
+ .end(function(err, res) {
+ if(err) done(err);
+ res.ok.should.be.ok;
+ done();
+ });
+ });
+
+ it("should return the forwarded host and port", function(done) {
+ request(app)
+ .get("/")
+ .set("X-Forwarded-Host", "example.com")
+ .set("X-Forwarded-Port", "8080")
+ .expect("http://example.com:8080")
+ .end(function(err, res) {
+ if(err) done(err);
+ res.ok.should.be.ok;
+ done();
+ });
+ });
+
+ it("should return the forwarded host, port and path", function(done) {
+ request(app)
+ .get("/")
+ .set("X-Forwarded-Host", "example.com")
+ .set("X-Forwarded-Port", "8080")
+ .set("X-Forwarded-Path", "/testing")
+ .expect("http://example.com:8080/testing")
+ .end(function(err, res) {
+ if(err) done(err);
+ res.ok.should.be.ok;
+ done();
+ });
+ });
+
+ it("should return the forwarded proto, host, port and path", function(done) {
+ request(app)
+ .get("/")
+ .set("X-Forwarded-Proto", "https")
+ .set("X-Forwarded-Host", "example.com")
+ .set("X-Forwarded-Port", "8080")
+ .set("X-Forwarded-Path", "/testing")
+ .expect("https://example.com:8080/testing")
+ .end(function(err, res) {
+ if(err) done(err);
+ res.ok.should.be.ok;
+ done();
+ });
+ });
+
+});

0 comments on commit f7ab006

Please sign in to comment.