rack-force_domain 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.markdown ADDED
@@ -0,0 +1,20 @@
1
+ # Rack::ForceDomain
2
+
3
+ Directs all traffic to a single domain via 301 redirects.
4
+
5
+ ## Example Usage (Heroku)
6
+
7
+ ### config.ru
8
+ use Rack::ForceDomain, ENV["DOMAIN"]
9
+
10
+ ### environment.rb
11
+ config.middleware.use Rack::ForceDomain, ENV["DOMAIN"]
12
+
13
+ ### Heroku Config
14
+
15
+ heroku config add DOMAIN="foo.com"
16
+
17
+
18
+ Now all requests to www.foo.com (or anything else pointed at the app) will 301 to foo.com.
19
+
20
+ If the `$DOMAIN` environment variable is missing, no redirection will occur.
@@ -0,0 +1 @@
1
+ require File.expand_path("rack/force_domain", __FILE__)
@@ -0,0 +1,18 @@
1
+ require 'rack'
2
+
3
+ class Rack::ForceDomain
4
+ def initialize(app, domain)
5
+ @app = app
6
+ @domain = domain
7
+ end
8
+
9
+ def call(env)
10
+ request = Rack::Request.new(env)
11
+ if @domain and request.host != @domain
12
+ fake_request = Rack::Request.new(env.merge("HTTP_HOST" => @domain))
13
+ Rack::Response.new([], 301, "Location" => fake_request.url).finish
14
+ else
15
+ @app.call(env)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,30 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), *%w[.. lib]))
2
+
3
+ require 'rack/test'
4
+ require 'test/unit'
5
+ require 'rack/force_domain'
6
+
7
+ class ForceDomainTest < Test::Unit::TestCase
8
+ include Rack::Test::Methods
9
+
10
+ def test_should_do_nothing_if_domain_is_null
11
+ app = Rack::ForceDomain.new(lambda{|env| @called = true; [200, [], {}] }, nil)
12
+ app.call(Rack::MockRequest.env_for("http://foo.com/baz"))
13
+ assert @called, "expected the request to be passed through"
14
+ end
15
+
16
+ def test_should_301_if_domain_is_wrong
17
+ app = Rack::ForceDomain.new(lambda{|env| @called = true; [200, [], {}] } , "bar.com")
18
+ status, body, headers = app.call(Rack::MockRequest.env_for("http://foo.com/baz"))
19
+ assert_equal 301, status
20
+ assert_equal "http://bar.com/baz", headers["Location"]
21
+ assert !@called, "should not have passed through"
22
+ end
23
+
24
+ def test_should_passthrough_for_correct_domain
25
+ app = Rack::ForceDomain.new(lambda{|env| @called = true; [200, [], {}] } , "foo.com")
26
+ status, body, headers = app.call(Rack::MockRequest.env_for("http://foo.com/baz"))
27
+ assert_equal 200, status
28
+ assert @called, "expected the request to be passed through"
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-force_domain
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tom Lea
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-29 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: contrib@tomlea.co.uk
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.markdown
24
+ files:
25
+ - README.markdown
26
+ - test/force_domain_test.rb
27
+ - lib/rack/force_domain.rb
28
+ - lib/rack-force_domain.rb
29
+ has_rdoc: true
30
+ homepage: http://tomlea.co.uk/
31
+ licenses: []
32
+
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --main
36
+ - README.markdown
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project: rack-force_domain
54
+ rubygems_version: 1.3.5
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Force all visitors onto a single domain.
58
+ test_files: []
59
+