require "#{File.expand_path(File.dirname(__FILE__))}/helper" describe "Router Basic" do before :all do class ::SomeController def call; end end end after :all do remove_constants %w( SomeController BlogControllerSpec ) end before :each do @router = Crystal::Router.new :class # @router.routes.push :default_router, Crystal::DefaultRouter.new @params = OpenObject.new @router.stub(:workspace).and_return({:params => @params}.to_openobject) end it "encode" do @router.encode(SomeController, :call, :format => :json).should == ["/some_controller/call.json", {}] end it "decode" do @router.decode("/some_controller/call", {}).should == [SomeController, 'call', {}] end describe "named routes" do before :all do class BlogControllerSpec def call; end end end before :each do @router.routes.unshift :named_router, Crystal::NamedRouter.new @router.routes[:named_router].add 'some/path', SomeController @router.routes[:named_router].add 'blog', BlogControllerSpec @named_router = @router.routes[:named_router] @helper = Object.new @helper.extend Crystal::NamedRouter::Helper end it "should encode and decode using named route" do @router.encode(SomeController, :call, :format => :json).should == ["/some/path/call.json", {}] @router.decode("/some/path/call", {}).should == [SomeController, 'call', {}] @router.encode(BlogControllerSpec, :call, :format => :json).should == ["/blog/call.json", {}] @router.decode("/blog/call", {}).should == [BlogControllerSpec, 'call', {}] end it "should generate __path methods (on the fly)" do @named_router.encode_route_method('show_blog_path').should == ["/blog/show", {}] lambda{@named_router.encode_route_method('show_unknown_path')}.should raise_error(/Unknown route/) end it "should provide helper" do crystal.register :router crystal[:router] = @router @helper.show_blog_path.should == ["/blog/show", {}] lambda{@helper.show_blog}.should raise_error(/undefined method/) end end describe "url_for" do it "should convert complex params to json" do @router.url_for(SomeController, :call, :a => :b, :as_json => true).should == %(/some_controller/call?json=%7B%22a%22%3A%22b%22%7D) end it "should escape params" do @router.url_for(SomeController, :call, :a => 'b/c').should == "/some_controller/call?a=b%2Fc" end it "url_for_remote" do @router.stub(:current_class).and_return(SomeController) @router.url_for(SomeController, :call, :format => :json).should == "/some_controller/call.json" @router.url_for(:call, :format => :json).should == "/some_controller/call.json" @router.url_for(SomeController, :call).should == "/some_controller/call" @router.url_for(:call).should == "/some_controller/call" @router.url_for('call').should == "/some_controller/call" end it "url_for_path should recognize first slash as to use url_for_path" do @router.url_for('/some_path', :key => :value).should == "/some_path?key=value" @router.url_for('/some_path').should == "/some_path" end it "should mark url string with format" do @router.stub(:current_class).and_return(SomeController) @router.url_for(:call, :format => :js).marks.format.to_s.should == 'js' @router.url_for('/path').marks.format.should be_nil @router.url_for('/path', :format => :js).marks.format.to_s.should == 'js' end end describe "persistent params" do before :each do @router.global_persistent_params << :l end it "global persistent params" do @params.l, @params.id = 'ru', 1 @router.url_for('/path').should == "/path?l=ru" @router.url_for('/path', :l => 'en').should == "/path?l=en" end it "should persist params with begining underscore" do @params._space, @params.id = 'space_id', 1 @router.url_for('/path').should == '/path' @router.persist_params do @router.url_for('/path').should == '/path?_space=space_id' @router.url_for('/path', :_space => 'another_id').should == '/path?_space=another_id' end end end end