spec/private/recognize_spec.rb in usher-0.5.3 vs spec/private/recognize_spec.rb in usher-0.5.4

- old
+ new

@@ -29,18 +29,21 @@ route_set.add_route('/sample', :controller => 'sample', :action => 'action2', :conditions => {:domain => 'www.host.com'}) route_set.recognize(build_request({:method => 'get', :path => '/sample', :domain => 'admin.host.com'})).path.route.should == target_route end it "should recognize a specific route when several http-style restrictions are used" do + target_route_http_admin_generic = route_set.add_route('/sample', :conditions => {:domain => 'admin.spec.com'}) target_route_http_admin = route_set.add_route('/sample', :conditions => {:protocol => 'http', :domain => 'admin.spec.com'}) target_route_http_www = route_set.add_route('/sample', :conditions => {:protocol => 'http', :domain => 'www.spec.com'}) - target_route_https_msie = route_set.add_route('/sample', :conditions => {:protocol => 'https', :user_agent => 'MSIE 6.0'}) + target_route_https_msie = route_set.add_route('/sample', :conditions => {:protocol => 'https', :domain => 'admin.spec.com', :user_agent => 'MSIE 6.0'}) target_route_https_admin = route_set.add_route('/sample', :conditions => {:protocol => 'https', :domain => 'admin.spec.com'}) route_set.recognize(build_request({:method => 'get', :path => '/sample', :protocol => 'http', :domain => 'admin.spec.com', :user_agent => nil})).path.route.should == target_route_http_admin route_set.recognize(build_request({:method => 'get', :path => '/sample', :protocol => 'http', :domain => 'www.spec.com', :user_agent => nil})).path.route.should == target_route_http_www route_set.recognize(build_request({:method => 'get', :path => '/sample', :protocol => 'https', :domain => 'admin.spec.com', :user_agent => 'MSIE 6.0'})).path.route.should == target_route_https_msie route_set.recognize(build_request({:method => 'get', :path => '/sample', :protocol => 'https', :domain => 'admin.spec.com', :user_agent => nil})).path.route.should == target_route_https_admin + route_set.recognize(build_request({:method => 'put', :path => '/sample', :protocol => 'wacky', :domain => 'admin.spec.com', :user_agent => nil})).path.route.should == target_route_http_admin_generic + end it "should correctly fix that tree if conditionals are used later" do noop_route = route_set.add_route('/noop', :controller => 'products', :action => 'noop') product_show_route = route_set.add_route('/products/show/:id', :id => /\d+/, :conditions => {:method => 'get'}) @@ -176,12 +179,12 @@ route_set.recognize(build_request({:method => 'get', :path => '/sample.html', :domain => 'admin.host.com'})).should == Usher::Node::Response.new(target_route.paths.first, [[:action , 'sample'], [:format, 'html']], nil, '/sample.html') end it "should use a requirement (proc) on incoming variables" do route_set.add_route('/:controller/:action/:id', :id => proc{|v| Integer(v)}) - proc {route_set.recognize(build_request({:method => 'get', :path => '/products/show/123', :domain => 'admin.host.com'}))}.should_not raise_error Usher::ValidationException - proc {route_set.recognize(build_request({:method => 'get', :path => '/products/show/123asd', :domain => 'admin.host.com'}))}.should raise_error Usher::ValidationException + proc {route_set.recognize(build_request({:method => 'get', :path => '/products/show/123', :domain => 'admin.host.com'}))}.should_not raise_error(Usher::ValidationException) + proc {route_set.recognize(build_request({:method => 'get', :path => '/products/show/123asd', :domain => 'admin.host.com'}))}.should raise_error(Usher::ValidationException) end it "shouldn't care about mildly weird characters in the URL" do route = route_set.add_route('/!asd,qwe/hjk$qwe/:id') route_set.recognize(build_request({:method => 'get', :path => '/!asd,qwe/hjk$qwe/09AZaz$-_+!*\'', :domain => 'admin.host.com'})).params.rassoc('09AZaz$-_+!*\'').first.should == :id @@ -210,7 +213,54 @@ route_set.recognize(build_request({:method => 'get', :path => '/foo/bar'})).path.route.should == route route_set.recognize(build_request({:method => 'post', :path => '/foo/bar'})).should.nil? end + end + + describe "dup safety" do + before do + route_set.add_route("/foo", :foo => "foo") + @r2 = route_set.dup + end + + it "should provide a different object" do + route_set.should_not eql(@r2) + end + + it "should recognize the originals routes in the dup" do + route_set.recognize(build_request(:path => "/foo")).path.route.destination.should == {:foo =>"foo"} + @r2.recognize( build_request(:path => "/foo")).path.route.destination.should == {:foo =>"foo"} + end + + it "should not add routes added to the dup to the original" do + @r2.add_route("/bar", :bar => "bar") + @r2.recognize( build_request(:path => "/bar")).path.route.destination.should == {:bar => "bar"} + route_set.recognize( build_request(:path => "/bar")).should == nil + end + + it "should not delete routes added to the dup to the original" do + @r2.delete_route("/foo") + route_set.recognize( build_request(:path => "/foo")).path.route.destination.should == {:foo => "foo"} + @r2.recognize( build_request(:path => "/foo")).should == nil + end + + + it "should safely dup with nested ushers" do + r1 = Usher.new + r2 = Usher.new + r3 = Usher.new + + r1.add_route("/mounted" ).match_partially!.to(r2) + r2.add_route("/inner" ).match_partially!.to(r3) + r3.add_route("/baz", :baz => :baz ) + + r1.recognize(build_request(:path => "/mounted/inner")).path.route.destination.should == r2 + r4 = r1.dup + r4.recognize(build_request(:path => "/mounted/inner")).path.route.destination.should == r2 + r4.add_route("/r3").match_partially!.to(r3) + r4.recognize(build_request(:path => "/r3")).path.route.destination.should == r3 + r1.recognize(build_request(:path => "/r3")).should be_nil + end + end end \ No newline at end of file