rack-domain_director 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0b3128290cf876c189cb281c2c03fd56fea13155
4
+ data.tar.gz: 746ba361ff42f91623624435d9513f470c4f30c3
5
+ SHA512:
6
+ metadata.gz: 4e0d2c70288d51bf1a654ebc2e5b6cc2435329a45ca70a7c53d523a78d97f817cab29d5e2b0d59cf9faf1e2feb793634558323c07bb03dad53cbb8d528a0867b
7
+ data.tar.gz: 2dd7279840a49ab502b5516ad421887c3856651c25d673d841c65aa0ed3c4bf4240dbb31648ed5aced4fdf178f6771692f0ebafd6ae3a5327dfab784ff596623
@@ -0,0 +1,34 @@
1
+ require 'rack'
2
+ require 'rack/domain_director/request'
3
+
4
+ class Rack::DomainDirector
5
+ def initialize(app, opts = {})
6
+ @app = app
7
+ @to = opts.fetch(:to)
8
+ @from = opts.fetch(:from)
9
+ @status = opts.fetch(:status, 301)
10
+ @before_redirect = opts.fetch(:before_redirect, ->(req) {})
11
+ end
12
+
13
+ def call(env)
14
+ req = Rack::DomainDirector::Request.new(env)
15
+
16
+ if redirectable?(req)
17
+ redirect(req)
18
+ else
19
+ @app.call(env)
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def redirect(req)
26
+ @before_redirect.call(req)
27
+ req.host = req.host.sub(%r{#{ Regexp.escape(@from) }$}, @to)
28
+ [@status, {'Location' => req.url}, []]
29
+ end
30
+
31
+ def redirectable?(req)
32
+ req.host.end_with?(@from)
33
+ end
34
+ end
@@ -0,0 +1,11 @@
1
+ module Rack
2
+ class DomainDirector
3
+ class Request < ::Rack::Request
4
+ attr_writer :host
5
+
6
+ def host
7
+ @host || super
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class DomainDirector
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,81 @@
1
+ require 'rack'
2
+ require 'rack/mock'
3
+ require 'rack/test'
4
+ require 'minitest/autorun'
5
+ require 'rack/domain_director'
6
+
7
+ class Minitest::Test
8
+ include Rack::Test::Methods
9
+
10
+ def app
11
+ Rack::Lint.new(@app)
12
+ end
13
+
14
+ def mock_app(opts = {})
15
+ @app = Rack::Builder.new do
16
+ use Rack::DomainDirector,
17
+ from: '.net',
18
+ to: '.com',
19
+ status: opts.fetch(:status, 301),
20
+ before_redirect: opts.fetch(:before_redirect, ->(x){})
21
+
22
+ run ->(env) { [200, {}, ['Hello']] }
23
+ end
24
+ end
25
+ end
26
+
27
+ class TestDomainDirector < Minitest::Test
28
+ def setup
29
+ mock_app
30
+ end
31
+
32
+ def test_that_it_redirects_net
33
+ get 'http://example.net/123/abc'
34
+ assert_equal 'http://example.com/123/abc', last_response.location
35
+ assert_equal 301, last_response.status
36
+ end
37
+
38
+ def test_that_it_wont_redirect_com
39
+ get 'http://example.com/123/abc'
40
+ assert_equal 'Hello', last_response.body
41
+ assert_equal 200, last_response.status
42
+ end
43
+
44
+ def test_that_it_conserves_scheme
45
+ get 'https://example.net/123/abc'
46
+ assert_equal 'https://example.com/123/abc', last_response.location
47
+ end
48
+
49
+ def test_that_it_conserves_non_standard_ports
50
+ get 'http://example.net:9000/123/abc'
51
+ assert_equal 'http://example.com:9000/123/abc', last_response.location
52
+ end
53
+
54
+ def test_that_it_conserves_query_strings
55
+ get 'https://example.net/123/abc?a=b&c=d'
56
+ assert_equal 'https://example.com/123/abc?a=b&c=d', last_response.location
57
+ end
58
+ end
59
+
60
+ class TestDomainDirector_BeforeRedirectOption < Minitest::Test
61
+ def setup
62
+ @host = nil
63
+ mock_app before_redirect: ->(req) { @host = req.host }
64
+ end
65
+
66
+ def test_before_redirect
67
+ get 'http://example.net/123/abc'
68
+ assert_equal 'example.net', @host
69
+ end
70
+ end
71
+
72
+ class TestDomainDirector_StatusOption < Minitest::Test
73
+ def setup
74
+ mock_app status: 302
75
+ end
76
+
77
+ def test_that_it_modifies_status
78
+ get 'http://example.net/123/abc'
79
+ assert_equal 302, last_response.status
80
+ end
81
+ end
@@ -0,0 +1,19 @@
1
+ require 'minitest/autorun'
2
+ require 'rack/mock'
3
+ require 'rack/domain_director/request'
4
+
5
+ class TestDomainDirectorRequest < Minitest::Test
6
+ def setup
7
+ @req = Rack::DomainDirector::Request.new \
8
+ Rack::MockRequest.env_for('http://example.com:8080/')
9
+ end
10
+
11
+ def test_it_conserves_host
12
+ assert_equal 'example.com', @req.host
13
+ end
14
+
15
+ def test_it_modifies_host
16
+ @req.host = 'test.example.com'
17
+ assert_equal 'test.example.com', @req.host
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-domain_director
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Larry Fox
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Rack middleware for redirecting one domain to another.
28
+ email:
29
+ - l@rryfox.us
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/rack/domain_director.rb
35
+ - lib/rack/domain_director/request.rb
36
+ - lib/rack/domain_director/version.rb
37
+ - test/test_domain_director.rb
38
+ - test/test_domain_director_request.rb
39
+ homepage: https://github.com/larryfox/rack-domain_director
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '2.0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.4.1
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Rack middleware to redirect domains
63
+ test_files:
64
+ - test/test_domain_director.rb
65
+ - test/test_domain_director_request.rb