test/rule_test.rb in rack-rewrite-1.0.2 vs test/rule_test.rb in rack-rewrite-1.1.0
- old
+ new
@@ -1,6 +1,6 @@
-require File.join(File.dirname(__FILE__), 'test_helper')
+require 'test_helper'
class RuleTest < Test::Unit::TestCase
TEST_ROOT = File.dirname(__FILE__)
@@ -144,10 +144,50 @@
end
end
end
context 'Rule#matches' do
+ context 'Given rule with :not option which matches "from" string' do
+ setup do
+ @rule = Rack::Rewrite::Rule.new(:rewrite, /^\/features/, '/facial_features', :not => '/features')
+ end
+ should 'not match PATH_INFO of /features' do
+ assert !@rule.matches?(rack_env_for("/features"))
+ end
+ should 'match PATH_INFO of /features.xml' do
+ assert @rule.matches?(rack_env_for("/features.xml"))
+ end
+ end
+
+ context 'Given rule with :host option of testapp.com' do
+ setup do
+ @rule = Rack::Rewrite::Rule.new(:rewrite, /^\/features/, '/facial_features', :host => 'testapp.com')
+ end
+
+ should 'match PATH_INFO of /features and HOST of testapp.com' do
+ assert @rule.matches?(rack_env_for("/features", 'SERVER_NAME' => 'testapp.com'))
+ end
+
+ should 'not match PATH_INFO of /features and HOST of nottestapp.com' do
+ assert ! @rule.matches?(rack_env_for("/features", 'SERVER_NAME' => 'nottestapp.com'))
+ end
+ end
+
+ context 'Given rule with :method option of POST' do
+ setup do
+ @rule = Rack::Rewrite::Rule.new(:rewrite, '/features', '/facial_features', :method => 'POST')
+ end
+
+ should 'match PATH_INFO of /features and REQUEST_METHOD of POST' do
+ assert @rule.matches?(rack_env_for("/features", 'REQUEST_METHOD' => 'POST'))
+ end
+
+ should 'not match PATH_INFO of /features and REQUEST_METHOD of DELETE' do
+ assert ! @rule.matches?(rack_env_for("/features", 'REQUEST_METHOD' => 'DELETE'))
+ end
+ end
+
context 'Given any rule with a "from" string of /features' do
setup do
@rule = Rack::Rewrite::Rule.new(:rewrite, '/features', '/facial_features')
end
@@ -305,10 +345,10 @@
rule = Rack::Rewrite::Rule.new(:rewrite, %r{/person_(\d+)(.*)}, lambda {|match, env| "people-#{match[1].to_i * 3}#{match[2]}"})
assert_equal 'people-3?show_bio=1', rule.send(:interpret_to, rack_env_for('/person_1?show_bio=1'))
end
end
- def rack_env_for(url)
+ def rack_env_for(url, options = {})
components = url.split('?')
- {'PATH_INFO' => components[0], 'QUERY_STRING' => components[1] || ''}
+ {'PATH_INFO' => components[0], 'QUERY_STRING' => components[1] || ''}.merge(options)
end
end