rack-force_domain 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7989be2cdf424464752efd83db25bc9c4c5386ab
4
+ data.tar.gz: 8eb430c9cf27ed7bc73856ad9667dfee0d0c0586
5
+ SHA512:
6
+ metadata.gz: 3b691e982e97b819356eca3c0ec21c656243895c95e239cc261ae7c3d3bd579a67602cabe9dcbb4c530b2e40393cc9597b21d8cce07976f72ae8b6a9bfcb76cc
7
+ data.tar.gz: a0f790ad36368589a9ed7bd7967a95da15af717c5bf585000ec261e076129a8ef065d7ccd6e077ef086a0c9cb5671ae6b14a2b0ad8353a086495337dc280f6e9
@@ -1,32 +1,24 @@
1
1
  require 'rack'
2
2
 
3
3
  class Rack::ForceDomain
4
- def initialize(app, domain)
4
+ def initialize(app, host, alternate_domains = [])
5
5
  @app = app
6
- if domain and domain != ""
7
- @domain = domain
8
-
9
- # set host from domain
10
- domain_parts = domain.split(":")
11
- @host = domain_parts[0]
6
+ @host = host unless host.to_s.empty?
7
+ @allowable_domains = [@host, *alternate_domains].compact
8
+ end
12
9
 
13
- # add port if applicable
14
- if domain_parts[1]
15
- port = domain_parts[1].to_i
16
- @port = port if port > 0
17
- end
18
- end
10
+ def ok?(domain)
11
+ @host.nil? or @allowable_domains.include?(domain)
19
12
  end
20
13
 
21
14
  def call(env)
22
15
  request = Rack::Request.new(env)
23
- host_mismatch = (@host && request.host != @host)
24
- port_mismatch = (@port && request.port != @port)
25
- if host_mismatch or port_mismatch
26
- fake_request = Rack::Request.new(env.merge("HTTP_HOST" => @domain, "SERVER_PORT" => @port || request.port))
27
- Rack::Response.new([], 301, "Location" => fake_request.url).finish
28
- else
16
+
17
+ if ok? request.host
29
18
  @app.call(env)
19
+ else
20
+ fake_request = Rack::Request.new(env.merge("HTTP_HOST" => @host))
21
+ Rack::Response.new([], 301, "Location" => fake_request.url).finish
30
22
  end
31
23
  end
32
24
  end
@@ -21,18 +21,35 @@ class ForceDomainTest < Test::Unit::TestCase
21
21
  assert !@called, "should not have passed through"
22
22
  end
23
23
 
24
- def test_should_301_if_port_is_wrong
25
- app = Rack::ForceDomain.new(lambda{|env| @called = true; [200, [], {}] } , "foo.com:3000")
26
- status, body, headers = app.call(Rack::MockRequest.env_for("http://foo.com/baz"))
27
- assert_equal 301, status
28
- assert_equal "http://foo.com:3000/baz", headers["Location"]
29
- assert !@called, "should not have passed through"
30
- end
31
-
32
24
  def test_should_passthrough_for_correct_domain
33
25
  app = Rack::ForceDomain.new(lambda{|env| @called = true; [200, [], {}] } , "foo.com")
34
26
  status, body, headers = app.call(Rack::MockRequest.env_for("http://foo.com/baz"))
35
27
  assert_equal 200, status
36
28
  assert @called, "expected the request to be passed through"
37
29
  end
30
+
31
+ def test_should_allow_domains_listed_in_alternate_domains_through
32
+ app = Rack::ForceDomain.new(lambda{|env| @called = true; [200, [], {}] } , "foo.com", ["bar.com", "baz.com"])
33
+
34
+ @called = false
35
+ status, body, headers = app.call(Rack::MockRequest.env_for("http://bar.com/baz"))
36
+ assert_equal 200, status
37
+ assert @called, "expected the request to be passed through"
38
+
39
+ @called = false
40
+ status, body, headers = app.call(Rack::MockRequest.env_for("http://baz.com/baz"))
41
+ assert_equal 200, status
42
+ assert @called, "expected the request to be passed through"
43
+
44
+ status, body, headers = app.call(Rack::MockRequest.env_for("http://other.com/baz"))
45
+ assert_equal 301, status
46
+ assert_equal "http://foo.com/baz", headers["Location"]
47
+ end
48
+
49
+ def test_it_redirects_https_to_https
50
+ app = Rack::ForceDomain.new(lambda{|env| @called = true; [200, [], {}] } , "foo.com")
51
+ status, body, headers = app.call(Rack::MockRequest.env_for("https://bar.com/baz"))
52
+ assert_equal 301, status
53
+ assert_equal "https://foo.com/baz", headers["Location"]
54
+ end
38
55
  end
metadata CHANGED
@@ -1,71 +1,49 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: rack-force_domain
3
- version: !ruby/object:Gem::Version
4
- hash: 23
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 2
9
- - 0
10
- version: 0.2.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Tom Lea
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2011-04-16 00:00:00 +01:00
19
- default_executable:
11
+ date: 2014-03-12 00:00:00.000000000 Z
20
12
  dependencies: []
21
-
22
13
  description:
23
14
  email: contrib@tomlea.co.uk
24
15
  executables: []
25
-
26
16
  extensions: []
27
-
28
- extra_rdoc_files:
17
+ extra_rdoc_files:
29
18
  - README.markdown
30
- files:
19
+ files:
31
20
  - README.markdown
32
21
  - test/force_domain_test.rb
33
22
  - lib/rack/force_domain.rb
34
23
  - lib/rack-force_domain.rb
35
- has_rdoc: true
36
24
  homepage: http://tomlea.co.uk/
37
25
  licenses: []
38
-
26
+ metadata: {}
39
27
  post_install_message:
40
- rdoc_options:
28
+ rdoc_options:
41
29
  - --main
42
30
  - README.markdown
43
- require_paths:
31
+ require_paths:
44
32
  - lib
45
- required_ruby_version: !ruby/object:Gem::Requirement
46
- none: false
47
- requirements:
48
- - - ">="
49
- - !ruby/object:Gem::Version
50
- hash: 3
51
- segments:
52
- - 0
53
- version: "0"
54
- required_rubygems_version: !ruby/object:Gem::Requirement
55
- none: false
56
- requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- hash: 3
60
- segments:
61
- - 0
62
- version: "0"
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
63
43
  requirements: []
64
-
65
44
  rubyforge_project: rack-force_domain
66
- rubygems_version: 1.3.7
45
+ rubygems_version: 2.0.14
67
46
  signing_key:
68
- specification_version: 3
47
+ specification_version: 4
69
48
  summary: Force all visitors onto a single domain.
70
49
  test_files: []
71
-