rack-www 1.5.0 → 2.0.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: c1a573de69069e3e4dba5fa155a61171e7f2a5db
4
+ data.tar.gz: 1eccb6a5132455dac0d7a3e52870ef3fc8999292
5
+ SHA512:
6
+ metadata.gz: 793e64555ba1265be7ac95a543920aeb2156a64636153ae6f16b35bf77393b40638f886b0c38227a778c1f35f0067a44064d451eafeade87450e049332ccde64
7
+ data.tar.gz: 4ae7ab8da7a192df653a069118b97db41b7b38a3f61c47c2b6f332dd4fdc9dbf3a452c1356d408d7e7d07236527c59239347bb58f0baa3a7395b6c77db3fa957
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ *.rbc
@@ -0,0 +1 @@
1
+ rack-www
@@ -0,0 +1 @@
1
+ 2.2.3
@@ -0,0 +1,34 @@
1
+ ## 2.0
2
+
3
+ - Update EVERYTHING
4
+
5
+ ## 1.5
6
+
7
+ - Bug fixes
8
+
9
+ ## 1.4
10
+
11
+ - Respect server port
12
+ - Body responds to :each
13
+ - Rack::Lint on tests
14
+
15
+ ## 1.3
16
+
17
+ - Added possibility to redirects to any given :subdomain [Ryan Weald (https://github.com/rweald)]
18
+ - Added more tests
19
+
20
+ ## 1.2
21
+
22
+ - Redirects to the right url without calling the app
23
+ - Keep the path when redirecting
24
+ - Keep the query string when redirecting
25
+ - Added more tests
26
+
27
+ ## 1.1
28
+
29
+ - Added possibility to redirects with www or without www
30
+ - Added possibility to set a param :message to show while redirecting
31
+
32
+ ## 1.0
33
+
34
+ - Redirects all traffic to www
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rack-www (2.0.0)
5
+ rack (~> 1.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ minitest (5.8.3)
11
+ rack (1.6.4)
12
+ rack-test (0.6.3)
13
+ rack (>= 1.0)
14
+ rake (10.4.2)
15
+
16
+ PLATFORMS
17
+ ruby
18
+
19
+ DEPENDENCIES
20
+ minitest (~> 5.8)
21
+ rack-test (~> 0.6)
22
+ rack-www!
23
+ rake (~> 10.4)
24
+
25
+ BUNDLED WITH
26
+ 1.10.6
@@ -1,55 +1,65 @@
1
- = rack-www
1
+ ## rack-www
2
2
 
3
- Rack middleware to force subdomain redirects, e.g: www.
3
+ Rack middleware to force subdomain redirects, e.g: www or www2
4
4
 
5
- === Installation
5
+ ### Installation
6
6
 
7
+ ```ruby
7
8
  #default installation
8
9
  gem install rack-www
9
10
 
10
11
  #when using bundler
11
12
  gem 'rack-www'
13
+ ```
12
14
 
13
-
14
- === Usage
15
+ ### Usage
15
16
 
16
17
  Default usage (by default will redirect all requests to www subdomain):
17
18
 
19
+ ```ruby
18
20
  #redirects all traffic to www subdomain
19
21
  config.middleware.use Rack::WWW
22
+ ```
20
23
 
21
24
  Customizing the :www option to true or false:
22
25
 
26
+ ```ruby
23
27
  #redirects all traffic to www
24
- config.middleware.use Rack::WWW, :www => true
28
+ config.middleware.use Rack::WWW, www: true
25
29
 
26
30
 
27
31
  #redirects all traffic to the same domain without www
28
- config.middleware.use Rack::WWW, :www => false
32
+ config.middleware.use Rack::WWW, www: false
33
+ ```
29
34
 
30
35
  Redirecting to a given subdomain:
31
36
 
37
+ ```ruby
32
38
  #redirects all traffic to the 'secure' subdomain
33
39
  config.middleware.use Rack::WWW, :subdomain => "secure"
40
+ ```
34
41
 
35
42
  If you like it's also possible to show a message while redirecting the user:
36
43
 
44
+ ```ruby
37
45
  config.middleware.use Rack::WWW, :www => false, :message => "You are being redirected..."
46
+ ```
38
47
 
39
48
  You can optionally specify predicate to determine if redirect should take place:
40
49
 
50
+ ```ruby
41
51
  config.middleware.use Rack::WWW, :predicate => lambda { |env|
42
52
  !Rack::Request.new(env).params.has_key? "noredirect"
43
53
  }
54
+ ```
44
55
 
56
+ ### Options
45
57
 
46
- === Options
47
-
48
- * :www => default is true, redirects all traffic to www;
49
- * :subdomain => redirects to any given subdomain;
50
- * :message => display a message while redirecting;
58
+ - :www => default is true, redirects all traffic to www;
59
+ - :subdomain => redirects to any given subdomain;
60
+ - :message => display a message while redirecting;
51
61
 
52
62
 
53
- === License
63
+ ### License
54
64
 
55
- MIT License. Copyright 2011 Jhimy Fernandes Villar. http://www.stjhimy.com
65
+ MIT License. Copyright 2011 Jhimy Fernandes Villar http://stjhimy.com
@@ -0,0 +1,9 @@
1
+ require 'rake/testtask'
2
+
3
+ task :default => :test
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.pattern = "test/**/*_test.rb"
8
+ t.warning = true
9
+ end
@@ -4,11 +4,10 @@ require 'rack/request'
4
4
  module Rack
5
5
  class WWW
6
6
  def initialize(app, options = {})
7
- @options = {:subdomain => "www" }.merge(options)
7
+ @options = {:subdomain => "www"}.merge(options)
8
8
  @app = app
9
9
 
10
- @redirect = true
11
- @redirect = @options[:www] if @options[:www] != nil
10
+ @redirect = @options[:www] != nil ? @options[:www] : true
12
11
  @message = @options[:message]
13
12
  @subdomain = @options[:subdomain]
14
13
  @predicate = @options[:predicate]
@@ -23,7 +22,6 @@ module Rack
23
22
  end
24
23
 
25
24
  private
26
-
27
25
  def redirect(env)
28
26
  url = prepare_url(env)
29
27
  headers = {"Content-Type" => "text/html", "location" => url}
@@ -50,29 +48,20 @@ module Rack
50
48
 
51
49
  def change_subdomain?(env)
52
50
  @redirect && !already_subdomain?(env) ||
53
- !@redirect && already_subdomain?(env)
51
+ !@redirect && already_subdomain?(env)
54
52
  end
55
53
 
56
54
  def already_subdomain?(env)
57
- env["HTTP_HOST"].downcase =~ /^(#{@subdomain}.)/
55
+ env["HTTP_HOST"].to_s.downcase =~ /^(#{@subdomain}\.)/
58
56
  end
59
57
 
60
58
  def prepare_url(env)
61
59
  scheme = env["rack.url_scheme"]
62
-
63
- host = env["SERVER_NAME"].gsub(/^(#{@subdomain}.)/, "")
64
- host = host.gsub(/^(www.)/, "")
65
-
66
- if env['SERVER_PORT'] == '80'
67
- port = ''
68
- else
69
- port = ":#{env['SERVER_PORT']}"
70
- end
71
-
60
+ host = env["SERVER_NAME"].to_s.gsub(/^(#{@subdomain}\.)/, "").gsub(/^(www\.)/, "")
61
+ port = env['SERVER_PORT'] == '80' ? '' : ":#{env['SERVER_PORT']}"
72
62
  path = env["PATH_INFO"]
73
63
 
74
- query_string = ""
75
- if !env["QUERY_STRING"].empty?
64
+ unless env["QUERY_STRING"].empty?
76
65
  query_string = "?" + env["QUERY_STRING"]
77
66
  end
78
67
 
@@ -81,6 +70,7 @@ module Rack
81
70
  else
82
71
  host = "://" + host
83
72
  end
73
+
84
74
  "#{scheme}#{host}#{port}#{path}#{query_string}"
85
75
  end
86
76
  end
@@ -0,0 +1,21 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'rack-www'
3
+ s.version = '2.0.0'
4
+ s.date = '2015-12-02'
5
+ s.homepage = "https://github.com/stjhimy/rack-www"
6
+ s.email = "stjhimy@gmail.com"
7
+ s.summary = "Force redirects to a any given subdomain, example: www or www2"
8
+ s.description = "Rack middleware to force subdomain redirects."
9
+ s.licenses = ["MIT"]
10
+ s.authors = ["Jhimy Fernandes Villar"]
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+ s.test_files = `git ls-files -- test/*`.split("\n")
14
+ s.require_paths = ["lib"]
15
+ s.required_ruby_version = '>= 1.9.3'
16
+
17
+ s.add_runtime_dependency 'rack', '~> 1.0'
18
+ s.add_development_dependency 'rake', '~> 10.4'
19
+ s.add_development_dependency 'rack-test', '~> 0.6'
20
+ s.add_development_dependency 'minitest', '~> 5.8'
21
+ end
@@ -0,0 +1,21 @@
1
+ require "rack/www"
2
+ require 'minitest/autorun'
3
+ require 'rack/test'
4
+
5
+ class TestClass < MiniTest::Test
6
+ include Rack::Test::Methods
7
+
8
+ def default_app
9
+ lambda { |env|
10
+ headers = {'Content-Type' => "text/html"}
11
+ headers['Set-Cookie'] = "id=1; path=/\ntoken=abc; path=/; secure; HttpOnly"
12
+ [200, headers, ["default body"]]
13
+ }
14
+ end
15
+
16
+ def app
17
+ @app ||= Rack::Lint.new(Rack::WWW.new(default_app))
18
+ end
19
+
20
+ attr_writer :app
21
+ end
@@ -0,0 +1,20 @@
1
+ require "helper"
2
+
3
+ class PredicateTest < TestClass
4
+ def test_predicate_false
5
+ self.app = Rack::WWW.new(default_app, :predicate => lambda { |env| false } )
6
+ get_example
7
+ assert_equal last_response.status, 200
8
+ end
9
+
10
+ def test_predicate_true
11
+ self.app = Rack::WWW.new(default_app, :predicate => lambda { |env| true } )
12
+ get_example
13
+ assert_equal "http://www.example.com/", last_response.headers['Location']
14
+ end
15
+
16
+ private
17
+ def get_example
18
+ get "http://example.com/"
19
+ end
20
+ end
@@ -0,0 +1,43 @@
1
+ require "helper"
2
+
3
+ class SubdomainTest < TestClass
4
+ def setup
5
+ self.app = Rack::WWW.new(default_app, :subdomain => "secure")
6
+ end
7
+
8
+ def test_if_allow_custom_subdomain
9
+ get 'http://example.com'
10
+ assert_equal 'http://secure.example.com/', last_response.headers['Location']
11
+ end
12
+
13
+ def test_custom_subdomain_with_path
14
+ get "http://example.com/path/1"
15
+ assert_equal "http://secure.example.com/path/1", last_response.headers['Location']
16
+ end
17
+
18
+ def test_custom_subdomain_with_path_and_www
19
+ get "http://www.example.com/path/1"
20
+ assert_equal "http://secure.example.com/path/1", last_response.headers['Location']
21
+ end
22
+
23
+ def test_custom_subdomain_with_query
24
+ get "http://example.com/path/1?test=true"
25
+ assert_equal "http://secure.example.com/path/1?test=true", last_response.headers['Location']
26
+ end
27
+
28
+ def test_custom_subdomain_with_query_and_www
29
+ get "http://www.example.com/path/1?test=true"
30
+ assert_equal "http://secure.example.com/path/1?test=true", last_response.headers['Location']
31
+ end
32
+
33
+ def test_body_with_custom_subdomain_and_message
34
+ self.app = Rack::WWW.new(default_app, :subdomain => "secure", :message => "redirecting now!")
35
+ get "http://example.com/"
36
+ assert_equal last_response.body, "redirecting now!"
37
+ end
38
+
39
+ def test_body_with_custom_subdomain_without_message
40
+ get "http://example.com/"
41
+ assert_equal last_response.body, ""
42
+ end
43
+ end
@@ -0,0 +1,27 @@
1
+ require "helper"
2
+
3
+ class FalseWWW < TestClass
4
+ def setup
5
+ self.app = Rack::WWW.new(default_app, :www => false)
6
+ end
7
+
8
+ def test_www_false
9
+ get "http://www.example.com/"
10
+ assert_equal "http://example.com/", last_response.headers['Location']
11
+ end
12
+
13
+ def test_www_false_with_path
14
+ get "http://www.example.com/path/1"
15
+ assert_equal "http://example.com/path/1", last_response.headers['Location']
16
+ end
17
+
18
+ def test_www_false_with_query
19
+ get "http://www.example.com/path/1?param=test"
20
+ assert_equal "http://example.com/path/1?param=test", last_response.headers['Location']
21
+ end
22
+
23
+ def test_www_false_non_www
24
+ get "http://example.com/"
25
+ assert last_response.ok?
26
+ end
27
+ end
@@ -0,0 +1,58 @@
1
+ require "helper"
2
+
3
+ class TrueWWW < TestClass
4
+ def setup
5
+ self.app = Rack::WWW.new(default_app, :www => true)
6
+ end
7
+
8
+ def test_response_200
9
+ get "http://www.example.com"
10
+ assert_equal last_response.status, 200
11
+ end
12
+
13
+ def test_response_301
14
+ get "http://example.com"
15
+ assert_equal last_response.status, 301
16
+ end
17
+
18
+ def test_redirects
19
+ get "http://example.com/"
20
+ assert_equal "http://www.example.com/", last_response.headers['Location']
21
+ end
22
+
23
+ def test_www_true
24
+ get "http://example.com/"
25
+ assert_equal "http://www.example.com/", last_response.headers['Location']
26
+ end
27
+
28
+ def test_www_true_with_path
29
+ get "http://example.com/path/1"
30
+ assert_equal "http://www.example.com/path/1", last_response.headers['Location']
31
+ end
32
+
33
+ def test_www_true_with_query
34
+ get "http://example.com/path/1?param=test"
35
+ assert_equal "http://www.example.com/path/1?param=test", last_response.headers['Location']
36
+ end
37
+
38
+ def test_body_with_message
39
+ self.app = Rack::WWW.new(default_app, :www => true, :message => "redirecting now!")
40
+ get "http://example.com/"
41
+ assert_equal last_response.body, "redirecting now!"
42
+ end
43
+
44
+ def test_body_without_message
45
+ get "http://example.com/"
46
+ assert_equal last_response.body, ""
47
+ end
48
+
49
+ def test_server_port
50
+ get "http://example.com:8080/"
51
+ assert_equal "http://www.example.com:8080/",last_response.headers['Location']
52
+ end
53
+
54
+ def test_server_port_80
55
+ get "http://example.com:80/"
56
+ assert_equal "http://www.example.com/",last_response.headers['Location']
57
+ end
58
+ end
metadata CHANGED
@@ -1,67 +1,121 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-www
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
5
- prerelease:
4
+ version: 2.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Jhimy Fernandes Villar
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-06-25 00:00:00.000000000 Z
11
+ date: 2015-12-02 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rack
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
- version: '0'
19
+ version: '1.0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
- version: '0'
30
- description: ! ' Rack middleware to force subdomain redirects, e.g: www.
31
-
32
- '
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.4'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack-test
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.8'
69
+ description: Rack middleware to force subdomain redirects.
33
70
  email: stjhimy@gmail.com
34
71
  executables: []
35
72
  extensions: []
36
73
  extra_rdoc_files: []
37
74
  files:
75
+ - ".gitignore"
76
+ - ".ruby-gemset"
77
+ - ".ruby-version"
78
+ - CHANGELOG.md
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE
82
+ - README.md
83
+ - Rakefile
38
84
  - lib/rack-www.rb
39
85
  - lib/rack/www.rb
40
- - LICENSE
41
- - CHANGELOG.rdoc
42
- - README.rdoc
86
+ - rack-www.gemspec
87
+ - test/helper.rb
88
+ - test/predicate_test.rb
89
+ - test/subdomain_test.rb
90
+ - test/www_false_test.rb
91
+ - test/www_true_test.rb
43
92
  homepage: https://github.com/stjhimy/rack-www
44
- licenses: []
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
45
96
  post_install_message:
46
97
  rdoc_options: []
47
98
  require_paths:
48
99
  - lib
49
100
  required_ruby_version: !ruby/object:Gem::Requirement
50
- none: false
51
101
  requirements:
52
- - - ! '>='
102
+ - - ">="
53
103
  - !ruby/object:Gem::Version
54
- version: '0'
104
+ version: 1.9.3
55
105
  required_rubygems_version: !ruby/object:Gem::Requirement
56
- none: false
57
106
  requirements:
58
- - - ! '>='
107
+ - - ">="
59
108
  - !ruby/object:Gem::Version
60
109
  version: '0'
61
110
  requirements: []
62
111
  rubyforge_project:
63
- rubygems_version: 1.8.24
112
+ rubygems_version: 2.4.5.1
64
113
  signing_key:
65
- specification_version: 3
66
- summary: ! 'Force redirects to a any given subdomain, e.g: www.'
67
- test_files: []
114
+ specification_version: 4
115
+ summary: 'Force redirects to a any given subdomain, example: www or www2'
116
+ test_files:
117
+ - test/helper.rb
118
+ - test/predicate_test.rb
119
+ - test/subdomain_test.rb
120
+ - test/www_false_test.rb
121
+ - test/www_true_test.rb
@@ -1,26 +0,0 @@
1
- == 1.4
2
-
3
- * Respect server port
4
- * Body responds to :each
5
- * Rack::Lint on tests
6
-
7
- == 1.3
8
-
9
- * Added possibility to redirects to any given :subdomain [Ryan Weald (https://github.com/rweald)].
10
- * Added more tests
11
-
12
- == 1.2
13
-
14
- * Redirects to the right url without calling the app
15
- * Keep the path when redirecting
16
- * Keep the query string when redirecting
17
- * Added more tests
18
-
19
- == 1.1
20
-
21
- * Added possibility to redirects with www or without www.
22
- * Added possibility to set a param :message to show while redirecting.
23
-
24
- == 1.0
25
-
26
- * Redirects all traffic to www.