rack-rewrite-matches 1.3.3
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/Gemfile +7 -0
- data/History.rdoc +61 -0
- data/LICENSE +22 -0
- data/README.markdown +332 -0
- data/Rakefile +39 -0
- data/VERSION +1 -0
- data/lib/rack-rewrite.rb +1 -0
- data/lib/rack/rewrite.rb +28 -0
- data/lib/rack/rewrite/rule.rb +247 -0
- data/lib/rack/rewrite/version.rb +5 -0
- data/rack-rewrite.gemspec +54 -0
- data/test/geminstaller.yml +9 -0
- data/test/rack-rewrite_test.rb +136 -0
- data/test/rule_test.rb +429 -0
- data/test/test_helper.rb +13 -0
- metadata +132 -0
data/test/rule_test.rb
ADDED
@@ -0,0 +1,429 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class RuleTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
TEST_ROOT = File.dirname(__FILE__)
|
6
|
+
|
7
|
+
def self.should_pass_maintenance_tests
|
8
|
+
context 'and the maintenance file does in fact exist' do
|
9
|
+
setup { File.stubs(:exists?).returns(true) }
|
10
|
+
|
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')) }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.negative_lookahead_supported?
|
21
|
+
begin
|
22
|
+
require 'oniguruma'
|
23
|
+
rescue LoadError; end
|
24
|
+
RUBY_VERSION =~ /^1\.9/ || Object.const_defined?(:Oniguruma)
|
25
|
+
end
|
26
|
+
|
27
|
+
def negative_lookahead_regexp
|
28
|
+
if RUBY_VERSION =~ /^1\.9/
|
29
|
+
# have to use the constructor instead of the literal syntax b/c load errors occur in Ruby 1.8
|
30
|
+
Regexp.new("(.*)$(?<!css|png|jpg)")
|
31
|
+
else
|
32
|
+
Oniguruma::ORegexp.new("(.*)$(?<!css|png|jpg)")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context '#Rule#apply' do
|
37
|
+
supported_status_codes.each do |rule_type|
|
38
|
+
should "set Location header to result of #interpret_to for a #{rule_type}" do
|
39
|
+
rule = Rack::Rewrite::Rule.new(rule_type, %r{/abc}, '/def')
|
40
|
+
env = {'PATH_INFO' => '/abc'}
|
41
|
+
assert_equal rule.send(:interpret_to, '/abc'), rule.apply!(env)[1]['Location']
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
supported_status_codes.each do |rule_type|
|
46
|
+
should "include a link to the result of #interpret_to for a #{rule_type}" do
|
47
|
+
rule = Rack::Rewrite::Rule.new(rule_type, %r{/abc}, '/def')
|
48
|
+
env = {'PATH_INFO' => '/abc'}
|
49
|
+
assert_match /\/def/, rule.apply!(env)[2][0]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
should 'keep the QUERY_STRING when a 301 rule matches a URL with a querystring' do
|
54
|
+
rule = Rack::Rewrite::Rule.new(:r301, %r{/john(.*)}, '/yair$1')
|
55
|
+
env = {'PATH_INFO' => '/john', 'QUERY_STRING' => 'show_bio=1'}
|
56
|
+
assert_equal '/yair?show_bio=1', rule.apply!(env)[1]['Location']
|
57
|
+
end
|
58
|
+
|
59
|
+
should 'keep the QUERY_STRING when a rewrite rule that requires a querystring matches a URL with a querystring' do
|
60
|
+
rule = Rack::Rewrite::Rule.new(:rewrite, %r{/john(\?.*)}, '/yair$1')
|
61
|
+
env = {'PATH_INFO' => '/john', 'QUERY_STRING' => 'show_bio=1'}
|
62
|
+
rule.apply!(env)
|
63
|
+
assert_equal '/yair', env['PATH_INFO']
|
64
|
+
assert_equal 'show_bio=1', env['QUERY_STRING']
|
65
|
+
assert_equal '/yair?show_bio=1', env['REQUEST_URI']
|
66
|
+
end
|
67
|
+
|
68
|
+
should 'update the QUERY_STRING when a rewrite rule changes its value' do
|
69
|
+
rule = Rack::Rewrite::Rule.new(:rewrite, %r{/(\w+)\?show_bio=(\d)}, '/$1?bio=$2')
|
70
|
+
env = {'PATH_INFO' => '/john', 'QUERY_STRING' => 'show_bio=1'}
|
71
|
+
rule.apply!(env)
|
72
|
+
assert_equal '/john', env['PATH_INFO']
|
73
|
+
assert_equal 'bio=1', env['QUERY_STRING']
|
74
|
+
assert_equal '/john?bio=1', env['REQUEST_URI']
|
75
|
+
end
|
76
|
+
|
77
|
+
should 'set Content-Type header to text/html for a 301 and 302 request for a .html page' do
|
78
|
+
supported_status_codes.each do |rule_type|
|
79
|
+
rule = Rack::Rewrite::Rule.new(rule_type, %r{/abc}, '/def.html')
|
80
|
+
env = {'PATH_INFO' => '/abc'}
|
81
|
+
assert_equal 'text/html', rule.apply!(env)[1]['Content-Type']
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
should 'set Content-Type header to text/css for a 301 and 302 request for a .css page' do
|
86
|
+
supported_status_codes.each do |rule_type|
|
87
|
+
rule = Rack::Rewrite::Rule.new(rule_type, %r{/abc}, '/def.css')
|
88
|
+
env = {'PATH_INFO' => '/abc'}
|
89
|
+
assert_equal 'text/css', rule.apply!(env)[1]['Content-Type']
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
should 'set additional headers for a 301 and 302 request' do
|
94
|
+
[:r301, :r302].each do |rule_type|
|
95
|
+
rule = Rack::Rewrite::Rule.new(rule_type, %r{/abc}, '/def.css', {:headers => {'Cache-Control' => 'no-cache'}})
|
96
|
+
env = {'PATH_INFO' => '/abc'}
|
97
|
+
assert_equal 'no-cache', rule.apply!(env)[1]['Cache-Control']
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
should 'evaluate additional headers block once per redirect request' do
|
102
|
+
[:r301, :r302].each do |rule_type|
|
103
|
+
header_val = 'foo'
|
104
|
+
rule = Rack::Rewrite::Rule.new(rule_type, %r{/abc}, '/def.css', {:headers => lambda { {'X-Foobar' => header_val} } })
|
105
|
+
env = {'PATH_INFO' => '/abc'}
|
106
|
+
assert_equal 'foo', rule.apply!(env)[1]['X-Foobar']
|
107
|
+
header_val = 'bar'
|
108
|
+
assert_equal 'bar', rule.apply!(env)[1]['X-Foobar']
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
should 'evaluate additional headers block once per send file request' do
|
113
|
+
[:send_file, :x_send_file].each do |rule_type|
|
114
|
+
header_val = 'foo'
|
115
|
+
rule = Rack::Rewrite::Rule.new(rule_type, /.*/, File.join(TEST_ROOT, 'geminstaller.yml'), {:headers => lambda { {'X-Foobar' => header_val} } })
|
116
|
+
env = {'PATH_INFO' => '/abc'}
|
117
|
+
assert_equal 'foo', rule.apply!(env)[1]['X-Foobar']
|
118
|
+
header_val = 'bar'
|
119
|
+
assert_equal 'bar', rule.apply!(env)[1]['X-Foobar']
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
context 'Given an :x_send_file rule that matches' do
|
124
|
+
setup do
|
125
|
+
@file = File.join(TEST_ROOT, 'geminstaller.yml')
|
126
|
+
@rule = Rack::Rewrite::Rule.new(:x_send_file, /.*/, @file, :headers => {'Cache-Control' => 'no-cache'})
|
127
|
+
env = {'PATH_INFO' => '/abc'}
|
128
|
+
@response = @rule.apply!(env)
|
129
|
+
end
|
130
|
+
|
131
|
+
should 'return 200' do
|
132
|
+
assert_equal 200, @response[0]
|
133
|
+
end
|
134
|
+
|
135
|
+
should 'return an X-Sendfile header' do
|
136
|
+
assert @response[1].has_key?('X-Sendfile')
|
137
|
+
end
|
138
|
+
|
139
|
+
should 'return a Content-Type of text/yaml' do
|
140
|
+
assert_equal 'text/yaml', @response[1]['Content-Type']
|
141
|
+
end
|
142
|
+
|
143
|
+
should 'return the proper Content-Length' do
|
144
|
+
assert_equal File.size(@file).to_s, @response[1]['Content-Length']
|
145
|
+
end
|
146
|
+
|
147
|
+
should 'return additional headers' do
|
148
|
+
assert_equal 'no-cache', @response[1]['Cache-Control']
|
149
|
+
end
|
150
|
+
|
151
|
+
should 'return empty content' do
|
152
|
+
assert_equal [], @response[2]
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
context 'Given a :send_file rule that matches' do
|
157
|
+
setup do
|
158
|
+
@file = File.join(TEST_ROOT, 'geminstaller.yml')
|
159
|
+
@rule = Rack::Rewrite::Rule.new(:send_file, /.*/, @file, :headers => {'Cache-Control' => 'no-cache'})
|
160
|
+
env = {'PATH_INFO' => '/abc'}
|
161
|
+
@response = @rule.apply!(env)
|
162
|
+
end
|
163
|
+
|
164
|
+
should 'return 200' do
|
165
|
+
assert_equal 200, @response[0]
|
166
|
+
end
|
167
|
+
|
168
|
+
should 'not return an X-Sendfile header' do
|
169
|
+
assert !@response[1].has_key?('X-Sendfile')
|
170
|
+
end
|
171
|
+
|
172
|
+
should 'return a Content-Type of text/yaml' do
|
173
|
+
assert_equal 'text/yaml', @response[1]['Content-Type']
|
174
|
+
end
|
175
|
+
|
176
|
+
should 'return the proper Content-Length' do
|
177
|
+
assert_equal File.size(@file).to_s, @response[1]['Content-Length']
|
178
|
+
end
|
179
|
+
|
180
|
+
should 'return additional headers' do
|
181
|
+
assert_equal 'no-cache', @response[1]['Cache-Control']
|
182
|
+
end
|
183
|
+
|
184
|
+
should 'return the contents of geminstaller.yml in an array for Ruby 1.9.2 compatibility' do
|
185
|
+
assert_equal [File.read(@file)], @response[2]
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
should 'return proper status for send_file or x_send_file if specified' do
|
190
|
+
[:send_file, :x_send_file].each do |rule_type|
|
191
|
+
file = File.join(TEST_ROOT, 'geminstaller.yml')
|
192
|
+
rule = Rack::Rewrite::Rule.new(rule_type, /.*/, file, :status => 503)
|
193
|
+
env = {'PATH_INFO' => '/abc'}
|
194
|
+
assert_equal 503, rule.apply!(env)[0]
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
end
|
199
|
+
|
200
|
+
context 'Rule#matches' do
|
201
|
+
context 'Given rule with :not option which matches "from" string' do
|
202
|
+
setup do
|
203
|
+
@rule = Rack::Rewrite::Rule.new(:rewrite, /^\/features/, '/facial_features', :not => '/features')
|
204
|
+
end
|
205
|
+
should 'not match PATH_INFO of /features' do
|
206
|
+
assert !@rule.matches?(rack_env_for("/features"))
|
207
|
+
end
|
208
|
+
should 'match PATH_INFO of /features.xml' do
|
209
|
+
assert @rule.matches?(rack_env_for("/features.xml"))
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
context 'Given rule with :host option of testapp.com' do
|
214
|
+
setup do
|
215
|
+
@rule = Rack::Rewrite::Rule.new(:rewrite, /^\/features/, '/facial_features', :host => 'testapp.com')
|
216
|
+
end
|
217
|
+
|
218
|
+
should 'match PATH_INFO of /features and HOST of testapp.com' do
|
219
|
+
assert @rule.matches?(rack_env_for("/features", 'SERVER_NAME' => 'testapp.com', "SERVER_PORT" => "8080"))
|
220
|
+
end
|
221
|
+
|
222
|
+
should 'not match PATH_INFO of /features and HOST of nottestapp.com' do
|
223
|
+
assert ! @rule.matches?(rack_env_for("/features", 'SERVER_NAME' => 'nottestapp.com', "SERVER_PORT" => "8080"))
|
224
|
+
end
|
225
|
+
|
226
|
+
should 'match PATH_INFO of /features AND HTTP_X_FORWARDED_HOST of testapp.com and SERVER_NAME of 127.0.0.1' do
|
227
|
+
assert @rule.matches?(rack_env_for("/features", "SERVER_NAME" => "127.0.0.1", "SERVER_PORT" => "8080", "HTTP_X_FORWARDED_HOST" => "testapp.com"))
|
228
|
+
end
|
229
|
+
|
230
|
+
should 'not match PATH_INFO of /features AND HTTP_X_FORWARDED_HOST of nottestapp.com and SERVER_NAME of 127.0.0.1' do
|
231
|
+
assert !@rule.matches?(rack_env_for("/features", "SERVER_NAME" => "127.0.0.1", "SERVER_PORT" => "8080", "HTTP_X_FORWARDED_HOST" => "nottestapp.com"))
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
context 'Given rule with :method option of POST' do
|
236
|
+
setup do
|
237
|
+
@rule = Rack::Rewrite::Rule.new(:rewrite, '/features', '/facial_features', :method => 'POST')
|
238
|
+
end
|
239
|
+
|
240
|
+
should 'match PATH_INFO of /features and REQUEST_METHOD of POST' do
|
241
|
+
assert @rule.matches?(rack_env_for("/features", 'REQUEST_METHOD' => 'POST'))
|
242
|
+
end
|
243
|
+
|
244
|
+
should 'not match PATH_INFO of /features and REQUEST_METHOD of DELETE' do
|
245
|
+
assert ! @rule.matches?(rack_env_for("/features", 'REQUEST_METHOD' => 'DELETE'))
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
context 'Given any rule with a "from" string of /features' do
|
250
|
+
setup do
|
251
|
+
@rule = Rack::Rewrite::Rule.new(:rewrite, '/features', '/facial_features')
|
252
|
+
end
|
253
|
+
|
254
|
+
should 'match PATH_INFO of /features' do
|
255
|
+
assert @rule.matches?(rack_env_for("/features"))
|
256
|
+
end
|
257
|
+
|
258
|
+
should 'not match PATH_INFO of /features.xml' do
|
259
|
+
assert !@rule.matches?(rack_env_for("/features.xml"))
|
260
|
+
end
|
261
|
+
|
262
|
+
should 'not match PATH_INFO of /my_features' do
|
263
|
+
assert !@rule.matches?(rack_env_for("/my_features"))
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
context 'Given a rule with the ^ operator' do
|
268
|
+
setup do
|
269
|
+
@rule = Rack::Rewrite::Rule.new(:rewrite, %r{^/jason}, '/steve')
|
270
|
+
end
|
271
|
+
should 'match with the ^ operator if match is at the beginning of the path' do
|
272
|
+
assert @rule.matches?(rack_env_for('/jason'))
|
273
|
+
end
|
274
|
+
|
275
|
+
should 'not match with the ^ operator when match is deeply nested' do
|
276
|
+
assert !@rule.matches?(rack_env_for('/foo/bar/jason'))
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
context 'Given any rule with a "from" regular expression of /features(.*)' do
|
281
|
+
setup do
|
282
|
+
@rule = Rack::Rewrite::Rule.new(:rewrite, %r{/features(.*)}, '/facial_features$1')
|
283
|
+
end
|
284
|
+
|
285
|
+
should 'match PATH_INFO of /features' do
|
286
|
+
assert @rule.matches?(rack_env_for("/features"))
|
287
|
+
end
|
288
|
+
|
289
|
+
should 'match PATH_INFO of /features.xml' do
|
290
|
+
assert @rule.matches?(rack_env_for('/features.xml'))
|
291
|
+
end
|
292
|
+
|
293
|
+
should 'match PATH_INFO of /features/1' do
|
294
|
+
assert @rule.matches?(rack_env_for('/features/1'))
|
295
|
+
end
|
296
|
+
|
297
|
+
should 'match PATH_INFO of /features?filter_by=name' do
|
298
|
+
assert @rule.matches?(rack_env_for('/features?filter_by_name=name'))
|
299
|
+
end
|
300
|
+
|
301
|
+
should 'match PATH_INFO of /features/1?hide_bio=1' do
|
302
|
+
assert @rule.matches?(rack_env_for('/features/1?hide_bio=1'))
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
context 'Given a rule with a guard that checks for the presence of a file' do
|
307
|
+
setup do
|
308
|
+
@rule = Rack::Rewrite::Rule.new(:rewrite, %r{(.)*}, '/maintenance.html', lambda { |rack_env|
|
309
|
+
File.exists?('maintenance.html')
|
310
|
+
})
|
311
|
+
end
|
312
|
+
|
313
|
+
context 'when the file exists' do
|
314
|
+
setup do
|
315
|
+
File.stubs(:exists?).returns(true)
|
316
|
+
end
|
317
|
+
|
318
|
+
should 'match' do
|
319
|
+
assert @rule.matches?(rack_env_for('/anything/should/match'))
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
context 'when the file does not exist' do
|
324
|
+
setup do
|
325
|
+
File.stubs(:exists?).returns(false)
|
326
|
+
end
|
327
|
+
|
328
|
+
should 'not match' do
|
329
|
+
assert !@rule.matches?(rack_env_for('/nothing/should/match'))
|
330
|
+
end
|
331
|
+
end
|
332
|
+
end
|
333
|
+
|
334
|
+
context 'Given the capistrano maintenance.html rewrite rule given in our README' do
|
335
|
+
setup do
|
336
|
+
@rule = Rack::Rewrite::Rule.new(:rewrite, /.*/, '/system/maintenance.html', lambda { |rack_env|
|
337
|
+
maintenance_file = File.join('system', 'maintenance.html')
|
338
|
+
File.exists?(maintenance_file) && rack_env['PATH_INFO'] !~ /\.(css|jpg|png)/
|
339
|
+
})
|
340
|
+
end
|
341
|
+
should_pass_maintenance_tests
|
342
|
+
end
|
343
|
+
|
344
|
+
if negative_lookahead_supported?
|
345
|
+
context 'Given the negative lookahead regular expression version of the capistrano maintenance.html rewrite rule given in our README' do
|
346
|
+
setup do
|
347
|
+
@rule = Rack::Rewrite::Rule.new(:rewrite, negative_lookahead_regexp, '/system/maintenance.html', lambda { |rack_env|
|
348
|
+
File.exists?(File.join('public', 'system', 'maintenance.html'))
|
349
|
+
})
|
350
|
+
end
|
351
|
+
should_pass_maintenance_tests
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
355
|
+
context 'Given the CNAME alternative rewrite rule in our README' do
|
356
|
+
setup do
|
357
|
+
@rule = Rack::Rewrite::Rule.new(:r301, %r{.*}, 'http://mynewdomain.com$&', lambda {|rack_env|
|
358
|
+
rack_env['SERVER_NAME'] != 'mynewdomain.com'
|
359
|
+
})
|
360
|
+
end
|
361
|
+
|
362
|
+
should 'match requests for domain myolddomain.com and redirect to mynewdomain.com' do
|
363
|
+
env = {'PATH_INFO' => '/anything', 'QUERY_STRING' => 'abc=1', 'SERVER_NAME' => 'myolddomain.com'}
|
364
|
+
assert @rule.matches?(env)
|
365
|
+
rack_response = @rule.apply!(env)
|
366
|
+
assert_equal 'http://mynewdomain.com/anything?abc=1', rack_response[1]['Location']
|
367
|
+
end
|
368
|
+
|
369
|
+
should 'not match requests for domain mynewdomain.com' do
|
370
|
+
assert !@rule.matches?({'PATH_INFO' => '/anything', 'SERVER_NAME' => 'mynewdomain.com'})
|
371
|
+
end
|
372
|
+
end
|
373
|
+
end
|
374
|
+
|
375
|
+
context 'Rule#interpret_to' do
|
376
|
+
should 'return #to when #from is a string' do
|
377
|
+
rule = Rack::Rewrite::Rule.new(:rewrite, '/abc', '/def')
|
378
|
+
assert_equal '/def', rule.send(:interpret_to, rack_env_for('/abc'))
|
379
|
+
end
|
380
|
+
|
381
|
+
should 'replace $1 on a match' do
|
382
|
+
rule = Rack::Rewrite::Rule.new(:rewrite, %r{/person_(\d+)}, '/people/$1')
|
383
|
+
assert_equal '/people/1', rule.send(:interpret_to, rack_env_for("/person_1"))
|
384
|
+
end
|
385
|
+
|
386
|
+
should 'be able to catch querystrings with a regexp match' do
|
387
|
+
rule = Rack::Rewrite::Rule.new(:rewrite, %r{/person_(\d+)(.*)}, '/people/$1$2')
|
388
|
+
assert_equal '/people/1?show_bio=1', rule.send(:interpret_to, rack_env_for('/person_1?show_bio=1'))
|
389
|
+
end
|
390
|
+
|
391
|
+
should 'be able to make 10 replacements' do
|
392
|
+
# regexp to reverse 10 characters
|
393
|
+
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')
|
394
|
+
assert_equal 'jihgfedcba', rule.send(:interpret_to, rack_env_for("abcdefghij"))
|
395
|
+
end
|
396
|
+
|
397
|
+
should 'replace $& on a match' do
|
398
|
+
rule = Rack::Rewrite::Rule.new(:rewrite, %r{.*}, 'http://example.org$&')
|
399
|
+
assert_equal 'http://example.org/person/1', rule.send(:interpret_to, rack_env_for("/person/1"))
|
400
|
+
end
|
401
|
+
|
402
|
+
should 'ignore empty captures' do
|
403
|
+
rule = Rack::Rewrite::Rule.new(:rewrite, %r{/person(_\d+)?}, '/people/$1')
|
404
|
+
assert_equal '/people/', rule.send(:interpret_to, rack_env_for("/person"))
|
405
|
+
end
|
406
|
+
|
407
|
+
should 'call to with from when it is a lambda' do
|
408
|
+
rule = Rack::Rewrite::Rule.new(:rewrite, 'a', lambda { |from, env| from * 2 })
|
409
|
+
assert_equal 'aa', rule.send(:interpret_to, rack_env_for('a'))
|
410
|
+
end
|
411
|
+
|
412
|
+
should 'call to with from match data' do
|
413
|
+
rule = Rack::Rewrite::Rule.new(:rewrite, %r{/person_(\d+)(.*)}, lambda {|match, env| "people-#{match[1].to_i * 3}#{match[2]}"})
|
414
|
+
assert_equal 'people-3?show_bio=1', rule.send(:interpret_to, rack_env_for('/person_1?show_bio=1'))
|
415
|
+
end
|
416
|
+
end
|
417
|
+
|
418
|
+
context 'Mongel 1.2.0.pre2 edge case: root url with a query string' do
|
419
|
+
should 'handle a nil PATH_INFO variable without errors' do
|
420
|
+
rule = Rack::Rewrite::Rule.new(:r301, '/a', '/')
|
421
|
+
assert_equal '?exists', rule.send(:build_path_from_env, {'QUERY_STRING' => 'exists'})
|
422
|
+
end
|
423
|
+
end
|
424
|
+
|
425
|
+
def rack_env_for(url, options = {})
|
426
|
+
components = url.split('?')
|
427
|
+
{'PATH_INFO' => components[0], 'QUERY_STRING' => components[1] || ''}.merge(options)
|
428
|
+
end
|
429
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-rewrite-matches
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.3.3
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Travis Jeffery
|
9
|
+
- John Trupiano
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-04-08 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: bundler
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0'
|
31
|
+
- !ruby/object:Gem::Dependency
|
32
|
+
name: shoulda
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ~>
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: 2.10.2
|
39
|
+
type: :development
|
40
|
+
prerelease: false
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.10.2
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: mocha
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.9.7
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ~>
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 0.9.7
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: rack
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
type: :development
|
72
|
+
prerelease: false
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
description: A rack middleware for enforcing rewrite rules. In many cases you can
|
80
|
+
get away with rack-rewrite instead of writing Apache mod_rewrite rules.
|
81
|
+
email: travisjeffery@gmail.com
|
82
|
+
executables: []
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files:
|
85
|
+
- LICENSE
|
86
|
+
- History.rdoc
|
87
|
+
files:
|
88
|
+
- History.rdoc
|
89
|
+
- LICENSE
|
90
|
+
- README.markdown
|
91
|
+
- Rakefile
|
92
|
+
- VERSION
|
93
|
+
- Gemfile
|
94
|
+
- lib/rack-rewrite.rb
|
95
|
+
- lib/rack/rewrite.rb
|
96
|
+
- lib/rack/rewrite/rule.rb
|
97
|
+
- lib/rack/rewrite/version.rb
|
98
|
+
- rack-rewrite.gemspec
|
99
|
+
- test/geminstaller.yml
|
100
|
+
- test/rack-rewrite_test.rb
|
101
|
+
- test/rule_test.rb
|
102
|
+
- test/test_helper.rb
|
103
|
+
homepage: http://github.com/jtrupiano/rack-rewrite
|
104
|
+
licenses: []
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options:
|
107
|
+
- --charset=UTF-8
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ! '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
none: false
|
118
|
+
requirements:
|
119
|
+
- - ! '>='
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project: johntrupiano
|
124
|
+
rubygems_version: 1.8.25
|
125
|
+
signing_key:
|
126
|
+
specification_version: 3
|
127
|
+
summary: A rack middleware for enforcing rewrite rules
|
128
|
+
test_files:
|
129
|
+
- test/rack-rewrite_test.rb
|
130
|
+
- test/geminstaller.yml
|
131
|
+
- test/rule_test.rb
|
132
|
+
- test/test_helper.rb
|