Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base: f33ada1632
...
compare: 2e4c4b0cd6
Checking mergeability… Don't worry, you can still create the pull request.
  • 8 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
View
1  .travis.yml
@@ -2,3 +2,4 @@ language: node_js
node_js:
- 0.4
- 0.6
+ - 0.7
View
2  LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2010 George Ornbo
+Copyright (c) 2011-12 George Ornbo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
View
4 Makefile
@@ -0,0 +1,4 @@
+test:
+ @./node_modules/.bin/mocha
+
+.PHONY: test
View
27 README.md
@@ -4,7 +4,7 @@
Inspired by [Rack::ForceDomain][1] it directs all traffic to a single domain with a 301 redirect.
-This is useful where you are hosting your [node][2] site with [Joyent][3], [Nodester][4] or [Bejes.us][5] and want to ensure all visitors are on the same domain.
+This is useful where you are hosting your [node][2] site with a PaaS provide like [Joyent][3] or [Nodester][4] and want to ensure all visitors are on the same domain.
## Installation
@@ -16,18 +16,27 @@ via [npm][6]
For [connect][7]
- var connect = require('connect'),
- force_domain = require('connect-force-domain');
-
- connect.createServer(force_domain('shapeshed.com')).listen(3000);
+ var connect = require('connect')
+ , http = require('http')
+ , forceDomain = require('connect-force-domain');
+ var app = connect()
+ .use(forceDomain('shapeshed.com'))
+
+ http.Server(app).listen(3000);
For [express][8]
- var express = require('express'),
- force_domain = require('connect-force-domain');
-
- express.createServer(force_domain('shapeshed.com')).listen(3000);
+ var express = require('express')
+ , forceDomain = require('connect-force-domain');
+
+ var app = module.exports = express.createServer();
+
+ app.configure(function(){
+ app.use(forceDomain('shapeshed.com');
+ });
+
+ app.listen(3000);
[1]: https://github.com/cwninja/rack-force_domain
[2]: http://nodejs.org/
View
18 lib/connect-force-domain.js
@@ -4,14 +4,14 @@
* Inspired by rack-force_domain
*
*/
-
module.exports = function(domain){
- domain = domain || false;
- return function(req, res, next){
- if (domain && (req.headers.host != domain)){
- res.writeHead(301, {"Location": 'http://' + domain + req.url});
- res.end();
- }
- next();
- }
+ domain = domain || false;
+ return function(req, res, next){
+ if (domain && (req.headers.host != domain)){
+ res.writeHead(301, {"Location": 'http://' + domain + req.url});
+ res.end();
+ } else {
+ next();
+ }
+ }
};
View
9 package.json
@@ -3,15 +3,16 @@
, "keywords": ["no-www", "rack", "middleware", "connect"]
, "main": "./index"
, "author": "George Ornbo <george@shapeshed.com> (http://shapeshed.com)"
-, "version": "0.0.3"
+, "version": "0.1.0"
+, "engines": { "node": "0.4 || 0.6 || 0.7" }
, "repository" :
{ "type" : "git"
, "url" : "https://github.com/shapeshed/connect-force-domain.git"}
, "devDependencies": {
- "expresso": ">=0.9.0",
- "connect": ">=1.7.3" }
+ "mocha": ">=0.14.1",
+ "connect": ">=2.0.0" }
, "bugs" : { "url" : "https://github.com/shapeshed/connect-force-domain/issues" }
-, "scripts": { "test": "expresso" }
+, "scripts": { "test": "make test" }
, "licenses" :
[ { "type" : "MIT"
, "url" : "https://github.com/shapeshed/connect-force-domain/raw/master/LICENSE"
View
94 test/connect-force-domain.test.js
@@ -1,35 +1,63 @@
+var connect = require('connect')
+ , http = require('http')
+ , assert = require('assert')
+ , forceDomain = require('../lib/connect-force-domain');
-var connect = require('connect'),
- force_domain = require('../lib/connect-force-domain'),
- assert = require('assert');
+var app = connect()
+ .use(forceDomain('shapeshed.com'))
-app = connect.createServer(force_domain('shapeshed.com'));
-
-
-module.exports = {
- 'GET /': function(){
- assert.response(app,
- { url: '/' },
- { status: 301 , headers: { 'Location': 'http://shapeshed.com/' }});
- },
- 'GET /foobar': function(){
- assert.response(app,
- { url: '/foobar' },
- { status: 301 , headers: { 'Location': 'http://shapeshed.com/foobar' }});
- },
- 'GET /foobar/': function(){
- assert.response(app,
- { url: '/foobar/' },
- { status: 301 , headers: { 'Location': 'http://shapeshed.com/foobar/' }});
- },
- 'GET /two/folders': function(){
- assert.response(app,
- { url: '/two/folders' },
- { status: 301 , headers: { 'Location': 'http://shapeshed.com/two/folders' }});
- },
- 'GET /two/folders/': function(){
- assert.response(app,
- { url: '/two/folders/' },
- { status: 301 , headers: { 'Location': 'http://shapeshed.com/two/folders/' }});
- }
-};
+describe('Connect Force Domain module', function(){
+ before(function(){
+ http.Server(app).listen(3000);
+ })
+ describe('making a get request', function(){
+ it('should get return a 301 response', function(done){
+ http.get( { host: '127.0.0.1', port: '3000' }, function(res){
+ assert.strictEqual(res.statusCode, 301);
+ done();
+ })
+ })
+ it('should set the location header correctly', function(done){
+ http.get( { host: '127.0.0.1', port: '3000' }, function(res){
+ assert.strictEqual(res.headers['location'], 'http://shapeshed.com/')
+ done();
+ })
+ })
+ it('should include a path in the location header', function(done){
+ http.get( { host: '127.0.0.1', port: '3000', path: '/foobar' }, function(res){
+ assert.strictEqual(res.headers['location'], 'http://shapeshed.com/foobar')
+ done();
+ })
+ })
+ it('should include trailing slashes in location header', function(done){
+ http.get( { host: '127.0.0.1', port: '3000', path: '/foobar/' }, function(res){
+ assert.strictEqual(res.headers['location'], 'http://shapeshed.com/foobar/')
+ done();
+ })
+ })
+ it('should include nested routes in location header', function(done){
+ http.get( { host: '127.0.0.1', port: '3000', path: '/foo/bar' }, function(res){
+ assert.strictEqual(res.headers['location'], 'http://shapeshed.com/foo/bar')
+ done();
+ })
+ })
+ it('should include nested routes with trailing slashes in location header', function(done){
+ http.get( { host: '127.0.0.1', port: '3000', path: '/foo/bar/' }, function(res){
+ assert.strictEqual(res.headers['location'], 'http://shapeshed.com/foo/bar/')
+ done();
+ })
+ })
+ it('should include a query string in the location header', function(done){
+ http.get( { host: '127.0.0.1', port: '3000', path: '/foo?q=1' }, function(res){
+ assert.strictEqual(res.headers['location'], 'http://shapeshed.com/foo?q=1')
+ done();
+ })
+ })
+ it('should include a query string after a trailing slash in the location header', function(done){
+ http.get( { host: '127.0.0.1', port: '3000', path: '/foo/?q=1' }, function(res){
+ assert.strictEqual(res.headers['location'], 'http://shapeshed.com/foo/?q=1')
+ done();
+ })
+ })
+ })
+})

No commit comments for this range

Something went wrong with that request. Please try again.