test/rule_test.rb in rack-rewrite-0.2.0 vs test/rule_test.rb in rack-rewrite-0.2.1
- old
+ new
@@ -36,31 +36,31 @@
rule = Rack::Rewrite::Rule.new(:r301, %r{/abc}, '/def')
env = {'PATH_INFO' => '/abc'}
assert_equal rule.send(:interpret_to, '/abc'), rule.apply!(env)[1]['Location']
end
- should 'keep the QUERYSTRING when a 301 rule matches a URL with a querystring' do
+ should 'keep the QUERY_STRING when a 301 rule matches a URL with a querystring' do
rule = Rack::Rewrite::Rule.new(:r301, %r{/john(.*)}, '/yair$1')
- env = {'REQUEST_URI' => '/john?show_bio=1', 'PATH_INFO' => '/john', 'QUERYSTRING' => 'show_bio=1'}
+ env = {'REQUEST_URI' => '/john?show_bio=1', 'PATH_INFO' => '/john', 'QUERY_STRING' => 'show_bio=1'}
assert_equal '/yair?show_bio=1', rule.apply!(env)[1]['Location']
end
- should 'keep the QUERYSTRING when a rewrite rule that requires a querystring matches a URL with a querystring' do
+ should 'keep the QUERY_STRING when a rewrite rule that requires a querystring matches a URL with a querystring' do
rule = Rack::Rewrite::Rule.new(:rewrite, %r{/john(\?.*)}, '/yair$1')
- env = {'REQUEST_URI' => '/john?show_bio=1', 'PATH_INFO' => '/john', 'QUERYSTRING' => 'show_bio=1'}
+ env = {'REQUEST_URI' => '/john?show_bio=1', 'PATH_INFO' => '/john', 'QUERY_STRING' => 'show_bio=1'}
rule.apply!(env)
assert_equal '/yair', env['PATH_INFO']
- assert_equal 'show_bio=1', env['QUERYSTRING']
+ assert_equal 'show_bio=1', env['QUERY_STRING']
assert_equal '/yair?show_bio=1', env['REQUEST_URI']
end
- should 'update the QUERYSTRING when a rewrite rule changes its value' do
+ should 'update the QUERY_STRING when a rewrite rule changes its value' do
rule = Rack::Rewrite::Rule.new(:rewrite, %r{/(\w+)\?show_bio=(\d)}, '/$1?bio=$2')
- env = {'REQUEST_URI' => '/john?show_bio=1', 'PATH_INFO' => '/john', 'QUERYSTRING' => 'show_bio=1'}
+ env = {'REQUEST_URI' => '/john?show_bio=1', 'PATH_INFO' => '/john', 'QUERY_STRING' => 'show_bio=1'}
rule.apply!(env)
assert_equal '/john', env['PATH_INFO']
- assert_equal 'bio=1', env['QUERYSTRING']
+ assert_equal 'bio=1', env['QUERY_STRING']
assert_equal '/john?bio=1', env['REQUEST_URI']
end
should 'set Content-Type header to text/html for a 301 and 302' do
[:r301, :r302].each do |rule_type|
@@ -204,11 +204,11 @@
context 'Given the capistrano maintenance.html rewrite rule given in our README' do
setup do
@rule = Rack::Rewrite::Rule.new(:rewrite, /.*/, '/system/maintenance.html', lambda { |rack_env|
maintenance_file = File.join('system', 'maintenance.html')
- File.exists?(maintenance_file) && !%w(css jpg png).any? {|ext| rack_env['REQUEST_URI'] =~ Regexp.new("\.#{ext}$")}
+ File.exists?(maintenance_file) && rack_env['REQUEST_URI'] !~ /\.(css|jpg|png)/
})
end
should_pass_maintenance_tests
end
@@ -220,10 +220,29 @@
})
end
should_pass_maintenance_tests
end
end
+
+ context 'Given the CNAME alternative rewrite rule in our README' do
+ setup do
+ @rule = Rack::Rewrite::Rule.new(:r301, %r{.*}, 'http://mynewdomain.com$&', lambda {|rack_env|
+ rack_env['SERVER_NAME'] != 'mynewdomain.com'
+ })
+ end
+
+ should 'match requests for domain myolddomain.com and redirect to mynewdomain.com' do
+ env = {'REQUEST_URI' => '/anything?abc=1', 'PATH_INFO' => '/anything', 'QUERY_STRING' => 'abc=1', 'SERVER_NAME' => 'myolddomain.com'}
+ assert @rule.matches?(env)
+ rack_response = @rule.apply!(env)
+ assert_equal 'http://mynewdomain.com/anything?abc=1', rack_response[1]['Location']
+ end
+
+ should 'not match requests for domain mynewdomain.com' do
+ assert !@rule.matches?({'REQUEST_URI' => '/anything', 'SERVER_NAME' => 'mynewdomain.com'})
+ end
+ end
end
context 'Rule#interpret_to' do
should 'return #to when #from is a string' do
rule = Rack::Rewrite::Rule.new(:rewrite, '/abc', '/def')
@@ -242,9 +261,19 @@
should 'be able to make 10 replacements' do
# regexp to reverse 10 characters
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')
assert_equal 'jihgfedcba', rule.send(:interpret_to, "abcdefghij")
+ end
+
+ should 'replace $& on a match' do
+ rule = Rack::Rewrite::Rule.new(:rewrite, %r{.*}, 'http://example.org$&')
+ assert_equal 'http://example.org/person/1', rule.send(:interpret_to, "/person/1")
+ end
+
+ should 'ignore empty captures' do
+ rule = Rack::Rewrite::Rule.new(:rewrite, %r{/person(_\d+)?}, '/people/$1')
+ assert_equal '/people/', rule.send(:interpret_to, "/person")
end
should 'call to with from when it is a lambda' do
rule = Rack::Rewrite::Rule.new(:rewrite, 'a', lambda { |from, env| from * 2 })
assert_equal 'aa', rule.send(:interpret_to, 'a')