rack-rewrite 0.2.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +6 -0
- data/README.rdoc +7 -6
- data/Rakefile +1 -3
- data/VERSION +1 -1
- data/lib/rack-rewrite.rb +1 -29
- data/lib/rack/rewrite.rb +27 -0
- data/lib/{rack-rewrite → rack/rewrite}/rule.rb +18 -6
- data/rack-rewrite.gemspec +21 -30
- data/test/geminstaller.yml +6 -6
- data/test/rule_test.rb +47 -30
- data/test/test_helper.rb +1 -3
- metadata +17 -19
- data/TODO +0 -9
data/History.rdoc
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
=== 1.0.0 / 2010-05-13
|
2
|
+
* API
|
3
|
+
* Fix rack 1.1.0 / rails3 compatibility by eliminating reliance on REQUEST_URI env param. Paths are now constructed with PATH_INFO and QUERY_STRING
|
4
|
+
* Follow rack directory/require convention: require 'rack/rewrite' instead of 'rack-rewrite'
|
5
|
+
* Include an HTML anchor tag linked to where the URL being redirected to in the body of 301's and 302's
|
6
|
+
|
1
7
|
=== 0.2.1 / 2010-01-06
|
2
8
|
* API
|
3
9
|
* Implement $& substitution pattern (thanks to {Ben Brinckerhoff}[http://github.com/bhb])
|
data/README.rdoc
CHANGED
@@ -15,7 +15,7 @@ can get away with rack-rewrite instead of writing Apache mod_rewrite rules.
|
|
15
15
|
=== Sample rackup file
|
16
16
|
|
17
17
|
gem 'rack-rewrite', '~> 0.2.1'
|
18
|
-
require 'rack
|
18
|
+
require 'rack/rewrite
|
19
19
|
use Rack::Rewrite do
|
20
20
|
rewrite '/wiki/John_Trupiano', '/john'
|
21
21
|
r301 '/wiki/Yair_Flicker', '/yair'
|
@@ -25,7 +25,7 @@ can get away with rack-rewrite instead of writing Apache mod_rewrite rules.
|
|
25
25
|
|
26
26
|
=== Sample usage in a rails app
|
27
27
|
config.gem 'rack-rewrite', '~> 0.2.1'
|
28
|
-
require 'rack
|
28
|
+
require 'rack/rewrite'
|
29
29
|
config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
|
30
30
|
rewrite '/wiki/John_Trupiano', '/john'
|
31
31
|
r301 '/wiki/Yair_Flicker', '/yair'
|
@@ -89,7 +89,7 @@ We can replace the mod_rewrite rules with the following Rack::Rewrite rule:
|
|
89
89
|
|
90
90
|
maintenance_file = File.join(RAILS_ROOT, 'public', 'system', 'maintenance.html')
|
91
91
|
send_file /.*/, maintenance_file, :if => Proc.new { |rack_env|
|
92
|
-
File.exists?(maintenance_file) && rack_env['
|
92
|
+
File.exists?(maintenance_file) && rack_env['PATH_INFO'] !~ /\.(css|jpg|png)/
|
93
93
|
}
|
94
94
|
|
95
95
|
If you're running Ruby 1.9, this rule is simplified:
|
@@ -111,9 +111,10 @@ get away with:
|
|
111
111
|
|
112
112
|
=== :rewrite
|
113
113
|
|
114
|
-
Calls to #rewrite will simply update the PATH_INFO and
|
115
|
-
values and pass the request onto the next chain in
|
116
|
-
that a user's browser will show will not be changed.
|
114
|
+
Calls to #rewrite will simply update the PATH_INFO, QUERY_STRING and
|
115
|
+
REQUEST_URI HTTP header values and pass the request onto the next chain in
|
116
|
+
the Rack stack. The URL that a user's browser will show will not be changed.
|
117
|
+
See these examples:
|
117
118
|
|
118
119
|
rewrite '/wiki/John_Trupiano', '/john' # [1]
|
119
120
|
rewrite %r{/wiki/(\w+)_\w+}, '/$1' # [2]
|
data/Rakefile
CHANGED
@@ -24,7 +24,7 @@ end
|
|
24
24
|
|
25
25
|
require 'rake/testtask'
|
26
26
|
Rake::TestTask.new(:test) do |test|
|
27
|
-
test.libs << 'lib' << 'test'
|
27
|
+
test.libs << 'lib' << 'test' << '.'
|
28
28
|
test.pattern = 'test/**/*_test.rb'
|
29
29
|
test.verbose = true
|
30
30
|
end
|
@@ -42,8 +42,6 @@ rescue LoadError
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
-
task :test => :check_dependencies
|
46
|
-
|
47
45
|
task :default => :test
|
48
46
|
|
49
47
|
require 'rake/rdoctask'
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
1.0.0
|
data/lib/rack-rewrite.rb
CHANGED
@@ -1,29 +1 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'rack-rewrite/rule'
|
4
|
-
|
5
|
-
module Rack
|
6
|
-
# A rack middleware for defining and applying rewrite rules. In many cases you
|
7
|
-
# can get away with rack-rewrite instead of writing Apache mod_rewrite rules.
|
8
|
-
class Rewrite
|
9
|
-
def initialize(app, &rule_block)
|
10
|
-
@app = app
|
11
|
-
@rule_set = RuleSet.new
|
12
|
-
@rule_set.instance_eval(&rule_block) if block_given?
|
13
|
-
end
|
14
|
-
|
15
|
-
def call(env)
|
16
|
-
if matched_rule = find_first_matching_rule(env)
|
17
|
-
rack_response = matched_rule.apply!(env)
|
18
|
-
# Don't invoke the app if applying the rule returns a rack response
|
19
|
-
return rack_response unless rack_response === true
|
20
|
-
end
|
21
|
-
@app.call(env)
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
25
|
-
def find_first_matching_rule(env) #:nodoc:
|
26
|
-
@rule_set.rules.detect { |rule| rule.matches?(env) }
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
1
|
+
require 'rack/rewrite'
|
data/lib/rack/rewrite.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rack/rewrite/rule'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
# A rack middleware for defining and applying rewrite rules. In many cases you
|
5
|
+
# can get away with rack-rewrite instead of writing Apache mod_rewrite rules.
|
6
|
+
class Rewrite
|
7
|
+
def initialize(app, &rule_block)
|
8
|
+
@app = app
|
9
|
+
@rule_set = RuleSet.new
|
10
|
+
@rule_set.instance_eval(&rule_block) if block_given?
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(env)
|
14
|
+
if matched_rule = find_first_matching_rule(env)
|
15
|
+
rack_response = matched_rule.apply!(env)
|
16
|
+
# Don't invoke the app if applying the rule returns a rack response
|
17
|
+
return rack_response unless rack_response === true
|
18
|
+
end
|
19
|
+
@app.call(env)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def find_first_matching_rule(env) #:nodoc:
|
24
|
+
@rule_set.rules.detect { |rule| rule.matches?(env) }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -71,7 +71,7 @@ module Rack
|
|
71
71
|
|
72
72
|
def matches?(rack_env) #:nodoc:
|
73
73
|
return false if !guard.nil? && !guard.call(rack_env)
|
74
|
-
path = rack_env
|
74
|
+
path = build_path_from_env(rack_env)
|
75
75
|
if self.is_a_regexp?(self.from)
|
76
76
|
path =~ self.from
|
77
77
|
elsif self.from.is_a?(String)
|
@@ -84,12 +84,12 @@ module Rack
|
|
84
84
|
# Either (a) return a Rack response (short-circuiting the Rack stack), or
|
85
85
|
# (b) alter env as necessary and return true
|
86
86
|
def apply!(env) #:nodoc:
|
87
|
-
interpreted_to = self.interpret_to(env
|
87
|
+
interpreted_to = self.interpret_to(env)
|
88
88
|
case self.rule_type
|
89
89
|
when :r301
|
90
|
-
[301, {'Location' => interpreted_to, 'Content-Type' => 'text/html'}, [
|
90
|
+
[301, {'Location' => interpreted_to, 'Content-Type' => 'text/html'}, [redirect_message(interpreted_to)]]
|
91
91
|
when :r302
|
92
|
-
[302, {'Location' => interpreted_to, 'Content-Type' => 'text/html'}, [
|
92
|
+
[302, {'Location' => interpreted_to, 'Content-Type' => 'text/html'}, [redirect_message(interpreted_to)]]
|
93
93
|
when :rewrite
|
94
94
|
# return [200, {}, {:content => env.inspect}]
|
95
95
|
env['REQUEST_URI'] = interpreted_to
|
@@ -118,7 +118,8 @@ module Rack
|
|
118
118
|
end
|
119
119
|
|
120
120
|
protected
|
121
|
-
def interpret_to(
|
121
|
+
def interpret_to(env) #:nodoc:
|
122
|
+
path = build_path_from_env(env)
|
122
123
|
return interpret_to_proc(path, env) if self.to.is_a?(Proc)
|
123
124
|
return computed_to(path) if compute_to?(path)
|
124
125
|
self.to
|
@@ -150,7 +151,18 @@ module Rack
|
|
150
151
|
computed_to.gsub!("$#{num}", match(path)[num].to_s)
|
151
152
|
end
|
152
153
|
return computed_to
|
153
|
-
end
|
154
|
+
end
|
155
|
+
|
156
|
+
# Construct the URL (without domain) from PATH_INFO and QUERY_STRING
|
157
|
+
def build_path_from_env(env)
|
158
|
+
path = env['PATH_INFO']
|
159
|
+
path += "?#{env['QUERY_STRING']}" unless env['QUERY_STRING'].nil? || env['QUERY_STRING'].empty?
|
160
|
+
path
|
161
|
+
end
|
162
|
+
|
163
|
+
def redirect_message(location)
|
164
|
+
%Q(Redirecting to <a href="#{location}">#{location}</a>)
|
165
|
+
end
|
154
166
|
end
|
155
167
|
end
|
156
168
|
end
|
data/rack-rewrite.gemspec
CHANGED
@@ -5,58 +5,49 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{rack-rewrite}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "1.0.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["John Trupiano"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-05-13}
|
13
13
|
s.description = %q{A rack middleware for enforcing rewrite rules. In many cases you can get away with rack-rewrite instead of writing Apache mod_rewrite rules.}
|
14
14
|
s.email = %q{jtrupiano@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
|
-
|
18
|
-
|
17
|
+
"History.rdoc",
|
18
|
+
"README.rdoc"
|
19
19
|
]
|
20
20
|
s.files = [
|
21
21
|
".document",
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
22
|
+
".gitignore",
|
23
|
+
"History.rdoc",
|
24
|
+
"LICENSE",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"lib/rack-rewrite.rb",
|
29
|
+
"lib/rack/rewrite.rb",
|
30
|
+
"lib/rack/rewrite/rule.rb",
|
31
|
+
"rack-rewrite.gemspec",
|
32
|
+
"test/geminstaller.yml",
|
33
|
+
"test/rack-rewrite_test.rb",
|
34
|
+
"test/rule_test.rb",
|
35
|
+
"test/test_helper.rb"
|
36
36
|
]
|
37
37
|
s.homepage = %q{http://github.com/jtrupiano/rack-rewrite}
|
38
38
|
s.rdoc_options = ["--charset=UTF-8"]
|
39
39
|
s.require_paths = ["lib"]
|
40
40
|
s.rubyforge_project = %q{johntrupiano}
|
41
|
-
s.rubygems_version = %q{1.3.
|
41
|
+
s.rubygems_version = %q{1.3.6}
|
42
42
|
s.summary = %q{A rack middleware for enforcing rewrite rules}
|
43
43
|
s.test_files = [
|
44
44
|
"test/rack-rewrite_test.rb",
|
45
|
-
|
46
|
-
|
45
|
+
"test/rule_test.rb",
|
46
|
+
"test/test_helper.rb"
|
47
47
|
]
|
48
48
|
|
49
49
|
if s.respond_to? :specification_version then
|
50
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
51
50
|
s.specification_version = 3
|
52
|
-
|
53
|
-
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
54
|
-
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
55
|
-
else
|
56
|
-
s.add_dependency(%q<shoulda>, [">= 0"])
|
57
|
-
end
|
58
|
-
else
|
59
|
-
s.add_dependency(%q<shoulda>, [">= 0"])
|
60
51
|
end
|
61
52
|
end
|
62
53
|
|
data/test/geminstaller.yml
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
gems:
|
2
|
-
- name: jeweler
|
3
|
-
version: '~> 1.3.0'
|
4
2
|
- name: shoulda
|
5
|
-
version: '
|
3
|
+
version: '= 2.10.3'
|
6
4
|
- name: mocha
|
7
|
-
version: '
|
8
|
-
- name:
|
9
|
-
version: '
|
5
|
+
version: '= 0.9.8'
|
6
|
+
- name: rack
|
7
|
+
version: '= 1.1.0'
|
8
|
+
# - name: oniguruma
|
9
|
+
# version: '= 1.1.0'
|
data/test/rule_test.rb
CHANGED
@@ -2,16 +2,18 @@ require File.join(File.dirname(__FILE__), 'test_helper')
|
|
2
2
|
|
3
3
|
class RuleTest < Test::Unit::TestCase
|
4
4
|
|
5
|
+
TEST_ROOT = File.dirname(__FILE__)
|
6
|
+
|
5
7
|
def self.should_pass_maintenance_tests
|
6
8
|
context 'and the maintenance file does in fact exist' do
|
7
9
|
setup { File.stubs(:exists?).returns(true) }
|
8
10
|
|
9
|
-
should('match for the root') { assert @rule.matches?(
|
10
|
-
should('match for a regular rails route') { assert @rule.matches?(
|
11
|
-
should('match for an html page') { assert @rule.matches?(
|
12
|
-
should('not match for a css file') { assert !@rule.matches?(
|
13
|
-
should('not match for a jpg file') { assert !@rule.matches?(
|
14
|
-
should('not match for a png file') { assert !@rule.matches?(
|
11
|
+
should('match for the root') { assert @rule.matches?(rack_env_for('/')) }
|
12
|
+
should('match for a regular rails route') { assert @rule.matches?(rack_env_for('/users/1')) }
|
13
|
+
should('match for an html page') { assert @rule.matches?(rack_env_for('/index.html')) }
|
14
|
+
should('not match for a css file') { assert !@rule.matches?(rack_env_for('/stylesheets/style.css')) }
|
15
|
+
should('not match for a jpg file') { assert !@rule.matches?(rack_env_for('/images/sls.jpg')) }
|
16
|
+
should('not match for a png file') { assert !@rule.matches?(rack_env_for('/images/sls.png')) }
|
15
17
|
end
|
16
18
|
end
|
17
19
|
|
@@ -38,15 +40,21 @@ class RuleTest < Test::Unit::TestCase
|
|
38
40
|
assert_equal rule.send(:interpret_to, '/abc'), rule.apply!(env)[1]['Location']
|
39
41
|
end
|
40
42
|
|
43
|
+
should 'include a link to the result of #interpret_to for a 301' do
|
44
|
+
rule = Rack::Rewrite::Rule.new(:r301, %r{/abc}, '/def')
|
45
|
+
env = {'PATH_INFO' => '/abc'}
|
46
|
+
assert_match /\/def/, rule.apply!(env)[2][0]
|
47
|
+
end
|
48
|
+
|
41
49
|
should 'keep the QUERY_STRING when a 301 rule matches a URL with a querystring' do
|
42
50
|
rule = Rack::Rewrite::Rule.new(:r301, %r{/john(.*)}, '/yair$1')
|
43
|
-
env = {'
|
51
|
+
env = {'PATH_INFO' => '/john', 'QUERY_STRING' => 'show_bio=1'}
|
44
52
|
assert_equal '/yair?show_bio=1', rule.apply!(env)[1]['Location']
|
45
53
|
end
|
46
54
|
|
47
55
|
should 'keep the QUERY_STRING when a rewrite rule that requires a querystring matches a URL with a querystring' do
|
48
56
|
rule = Rack::Rewrite::Rule.new(:rewrite, %r{/john(\?.*)}, '/yair$1')
|
49
|
-
env = {'
|
57
|
+
env = {'PATH_INFO' => '/john', 'QUERY_STRING' => 'show_bio=1'}
|
50
58
|
rule.apply!(env)
|
51
59
|
assert_equal '/yair', env['PATH_INFO']
|
52
60
|
assert_equal 'show_bio=1', env['QUERY_STRING']
|
@@ -55,7 +63,7 @@ class RuleTest < Test::Unit::TestCase
|
|
55
63
|
|
56
64
|
should 'update the QUERY_STRING when a rewrite rule changes its value' do
|
57
65
|
rule = Rack::Rewrite::Rule.new(:rewrite, %r{/(\w+)\?show_bio=(\d)}, '/$1?bio=$2')
|
58
|
-
env = {'
|
66
|
+
env = {'PATH_INFO' => '/john', 'QUERY_STRING' => 'show_bio=1'}
|
59
67
|
rule.apply!(env)
|
60
68
|
assert_equal '/john', env['PATH_INFO']
|
61
69
|
assert_equal 'bio=1', env['QUERY_STRING']
|
@@ -136,41 +144,46 @@ class RuleTest < Test::Unit::TestCase
|
|
136
144
|
end
|
137
145
|
|
138
146
|
should 'match PATH_INFO of /features' do
|
139
|
-
assert @rule.matches?(
|
147
|
+
assert @rule.matches?(rack_env_for("/features"))
|
140
148
|
end
|
141
149
|
|
142
150
|
should 'not match PATH_INFO of /features.xml' do
|
143
|
-
assert !@rule.matches?(
|
151
|
+
assert !@rule.matches?(rack_env_for("/features.xml"))
|
144
152
|
end
|
145
153
|
|
146
154
|
should 'not match PATH_INFO of /my_features' do
|
147
|
-
assert !@rule.matches?(
|
155
|
+
assert !@rule.matches?(rack_env_for("/my_features"))
|
148
156
|
end
|
149
157
|
end
|
150
158
|
|
159
|
+
should 'match with the ^ operator for regexps' do
|
160
|
+
rule = Rack::Rewrite::Rule.new(:rewrite, %r{^/jason}, '/steve')
|
161
|
+
assert rule.matches?(rack_env_for('/jason'))
|
162
|
+
end
|
163
|
+
|
151
164
|
context 'Given any rule with a "from" regular expression of /features(.*)' do
|
152
165
|
setup do
|
153
166
|
@rule = Rack::Rewrite::Rule.new(:rewrite, %r{/features(.*)}, '/facial_features$1')
|
154
167
|
end
|
155
168
|
|
156
169
|
should 'match PATH_INFO of /features' do
|
157
|
-
assert @rule.matches?(
|
170
|
+
assert @rule.matches?(rack_env_for("/features"))
|
158
171
|
end
|
159
172
|
|
160
173
|
should 'match PATH_INFO of /features.xml' do
|
161
|
-
assert @rule.matches?(
|
174
|
+
assert @rule.matches?(rack_env_for('/features.xml'))
|
162
175
|
end
|
163
176
|
|
164
177
|
should 'match PATH_INFO of /features/1' do
|
165
|
-
assert @rule.matches?(
|
178
|
+
assert @rule.matches?(rack_env_for('/features/1'))
|
166
179
|
end
|
167
180
|
|
168
181
|
should 'match PATH_INFO of /features?filter_by=name' do
|
169
|
-
assert @rule.matches?(
|
182
|
+
assert @rule.matches?(rack_env_for('/features?filter_by_name=name'))
|
170
183
|
end
|
171
184
|
|
172
185
|
should 'match PATH_INFO of /features/1?hide_bio=1' do
|
173
|
-
assert @rule.matches?(
|
186
|
+
assert @rule.matches?(rack_env_for('/features/1?hide_bio=1'))
|
174
187
|
end
|
175
188
|
end
|
176
189
|
|
@@ -187,7 +200,7 @@ class RuleTest < Test::Unit::TestCase
|
|
187
200
|
end
|
188
201
|
|
189
202
|
should 'match' do
|
190
|
-
assert @rule.matches?(
|
203
|
+
assert @rule.matches?(rack_env_for('/anything/should/match'))
|
191
204
|
end
|
192
205
|
end
|
193
206
|
|
@@ -197,7 +210,7 @@ class RuleTest < Test::Unit::TestCase
|
|
197
210
|
end
|
198
211
|
|
199
212
|
should 'not match' do
|
200
|
-
assert !@rule.matches?(
|
213
|
+
assert !@rule.matches?(rack_env_for('/nothing/should/match'))
|
201
214
|
end
|
202
215
|
end
|
203
216
|
end
|
@@ -206,7 +219,7 @@ class RuleTest < Test::Unit::TestCase
|
|
206
219
|
setup do
|
207
220
|
@rule = Rack::Rewrite::Rule.new(:rewrite, /.*/, '/system/maintenance.html', lambda { |rack_env|
|
208
221
|
maintenance_file = File.join('system', 'maintenance.html')
|
209
|
-
File.exists?(maintenance_file) && rack_env['
|
222
|
+
File.exists?(maintenance_file) && rack_env['PATH_INFO'] !~ /\.(css|jpg|png)/
|
210
223
|
})
|
211
224
|
end
|
212
225
|
should_pass_maintenance_tests
|
@@ -231,14 +244,14 @@ class RuleTest < Test::Unit::TestCase
|
|
231
244
|
end
|
232
245
|
|
233
246
|
should 'match requests for domain myolddomain.com and redirect to mynewdomain.com' do
|
234
|
-
env = {'
|
247
|
+
env = {'PATH_INFO' => '/anything', 'QUERY_STRING' => 'abc=1', 'SERVER_NAME' => 'myolddomain.com'}
|
235
248
|
assert @rule.matches?(env)
|
236
249
|
rack_response = @rule.apply!(env)
|
237
250
|
assert_equal 'http://mynewdomain.com/anything?abc=1', rack_response[1]['Location']
|
238
251
|
end
|
239
252
|
|
240
253
|
should 'not match requests for domain mynewdomain.com' do
|
241
|
-
assert !@rule.matches?({'
|
254
|
+
assert !@rule.matches?({'PATH_INFO' => '/anything', 'SERVER_NAME' => 'mynewdomain.com'})
|
242
255
|
end
|
243
256
|
end
|
244
257
|
end
|
@@ -246,44 +259,48 @@ class RuleTest < Test::Unit::TestCase
|
|
246
259
|
context 'Rule#interpret_to' do
|
247
260
|
should 'return #to when #from is a string' do
|
248
261
|
rule = Rack::Rewrite::Rule.new(:rewrite, '/abc', '/def')
|
249
|
-
assert_equal '/def', rule.send(:interpret_to, '/abc')
|
262
|
+
assert_equal '/def', rule.send(:interpret_to, rack_env_for('/abc'))
|
250
263
|
end
|
251
264
|
|
252
265
|
should 'replace $1 on a match' do
|
253
266
|
rule = Rack::Rewrite::Rule.new(:rewrite, %r{/person_(\d+)}, '/people/$1')
|
254
|
-
assert_equal '/people/1', rule.send(:interpret_to, "/person_1")
|
267
|
+
assert_equal '/people/1', rule.send(:interpret_to, rack_env_for("/person_1"))
|
255
268
|
end
|
256
269
|
|
257
270
|
should 'be able to catch querystrings with a regexp match' do
|
258
271
|
rule = Rack::Rewrite::Rule.new(:rewrite, %r{/person_(\d+)(.*)}, '/people/$1$2')
|
259
|
-
assert_equal '/people/1?show_bio=1', rule.send(:interpret_to, '/person_1?show_bio=1')
|
272
|
+
assert_equal '/people/1?show_bio=1', rule.send(:interpret_to, rack_env_for('/person_1?show_bio=1'))
|
260
273
|
end
|
261
274
|
|
262
275
|
should 'be able to make 10 replacements' do
|
263
276
|
# regexp to reverse 10 characters
|
264
277
|
rule = Rack::Rewrite::Rule.new(:rewrite, %r{(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)(\w)}, '$10$9$8$7$6$5$4$3$2$1')
|
265
|
-
assert_equal 'jihgfedcba', rule.send(:interpret_to, "abcdefghij")
|
278
|
+
assert_equal 'jihgfedcba', rule.send(:interpret_to, rack_env_for("abcdefghij"))
|
266
279
|
end
|
267
280
|
|
268
281
|
should 'replace $& on a match' do
|
269
282
|
rule = Rack::Rewrite::Rule.new(:rewrite, %r{.*}, 'http://example.org$&')
|
270
|
-
assert_equal 'http://example.org/person/1', rule.send(:interpret_to, "/person/1")
|
283
|
+
assert_equal 'http://example.org/person/1', rule.send(:interpret_to, rack_env_for("/person/1"))
|
271
284
|
end
|
272
285
|
|
273
286
|
should 'ignore empty captures' do
|
274
287
|
rule = Rack::Rewrite::Rule.new(:rewrite, %r{/person(_\d+)?}, '/people/$1')
|
275
|
-
assert_equal '/people/', rule.send(:interpret_to, "/person")
|
288
|
+
assert_equal '/people/', rule.send(:interpret_to, rack_env_for("/person"))
|
276
289
|
end
|
277
290
|
|
278
291
|
should 'call to with from when it is a lambda' do
|
279
292
|
rule = Rack::Rewrite::Rule.new(:rewrite, 'a', lambda { |from, env| from * 2 })
|
280
|
-
assert_equal 'aa', rule.send(:interpret_to, 'a')
|
293
|
+
assert_equal 'aa', rule.send(:interpret_to, rack_env_for('a'))
|
281
294
|
end
|
282
295
|
|
283
296
|
should 'call to with from match data' do
|
284
297
|
rule = Rack::Rewrite::Rule.new(:rewrite, %r{/person_(\d+)(.*)}, lambda {|match, env| "people-#{match[1].to_i * 3}#{match[2]}"})
|
285
|
-
assert_equal 'people-3?show_bio=1', rule.send(:interpret_to, '/person_1?show_bio=1')
|
298
|
+
assert_equal 'people-3?show_bio=1', rule.send(:interpret_to, rack_env_for('/person_1?show_bio=1'))
|
286
299
|
end
|
287
300
|
end
|
288
301
|
|
302
|
+
def rack_env_for(url)
|
303
|
+
components = url.split('?')
|
304
|
+
{'PATH_INFO' => components[0], 'QUERY_STRING' => components[1] || ''}
|
305
|
+
end
|
289
306
|
end
|
data/test/test_helper.rb
CHANGED
@@ -7,9 +7,7 @@ require 'mocha'
|
|
7
7
|
|
8
8
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
9
9
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
10
|
-
require 'rack
|
10
|
+
require 'rack/rewrite'
|
11
11
|
|
12
12
|
class Test::Unit::TestCase
|
13
13
|
end
|
14
|
-
|
15
|
-
TEST_ROOT = File.dirname(__FILE__)
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-rewrite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- John Trupiano
|
@@ -9,19 +14,10 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date: 2010-
|
17
|
+
date: 2010-05-13 00:00:00 -04:00
|
13
18
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
16
|
-
name: shoulda
|
17
|
-
type: :development
|
18
|
-
version_requirement:
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">="
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: "0"
|
24
|
-
version:
|
19
|
+
dependencies: []
|
20
|
+
|
25
21
|
description: A rack middleware for enforcing rewrite rules. In many cases you can get away with rack-rewrite instead of writing Apache mod_rewrite rules.
|
26
22
|
email: jtrupiano@gmail.com
|
27
23
|
executables: []
|
@@ -30,8 +26,8 @@ extensions: []
|
|
30
26
|
|
31
27
|
extra_rdoc_files:
|
32
28
|
- LICENSE
|
29
|
+
- History.rdoc
|
33
30
|
- README.rdoc
|
34
|
-
- TODO
|
35
31
|
files:
|
36
32
|
- .document
|
37
33
|
- .gitignore
|
@@ -39,10 +35,10 @@ files:
|
|
39
35
|
- LICENSE
|
40
36
|
- README.rdoc
|
41
37
|
- Rakefile
|
42
|
-
- TODO
|
43
38
|
- VERSION
|
44
39
|
- lib/rack-rewrite.rb
|
45
|
-
- lib/rack
|
40
|
+
- lib/rack/rewrite.rb
|
41
|
+
- lib/rack/rewrite/rule.rb
|
46
42
|
- rack-rewrite.gemspec
|
47
43
|
- test/geminstaller.yml
|
48
44
|
- test/rack-rewrite_test.rb
|
@@ -61,18 +57,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
61
57
|
requirements:
|
62
58
|
- - ">="
|
63
59
|
- !ruby/object:Gem::Version
|
60
|
+
segments:
|
61
|
+
- 0
|
64
62
|
version: "0"
|
65
|
-
version:
|
66
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
64
|
requirements:
|
68
65
|
- - ">="
|
69
66
|
- !ruby/object:Gem::Version
|
67
|
+
segments:
|
68
|
+
- 0
|
70
69
|
version: "0"
|
71
|
-
version:
|
72
70
|
requirements: []
|
73
71
|
|
74
72
|
rubyforge_project: johntrupiano
|
75
|
-
rubygems_version: 1.3.
|
73
|
+
rubygems_version: 1.3.6
|
76
74
|
signing_key:
|
77
75
|
specification_version: 3
|
78
76
|
summary: A rack middleware for enforcing rewrite rules
|
data/TODO
DELETED
@@ -1,9 +0,0 @@
|
|
1
|
-
* OUTSTANDING
|
2
|
-
* Add :host support to restrict which URL's a rewrite rule matches [10/15/09]
|
3
|
-
* Add support for specifying a config file instead of passing a block (e.g. config/rewrite.rb) [10/15/09]
|
4
|
-
* Better message than "Redirecting..." -- how about html that says where it's being redirected to? [10/16/09]
|
5
|
-
* Provide testing helpers (e.g. should_rewrite) to facilitate straightforward testing. [10/25/09]
|
6
|
-
* Allow rules to return arbitrary html (e.g. the contents of the maintenance page) [10/25/09]
|
7
|
-
|
8
|
-
* COMPLETED
|
9
|
-
* Add :if => lambda support for arbitrary conditional rule application (this will allow us to do the capistrano maintenance page w/o apache's mod_rewrite) [10/15/09]
|