require 'router/spec_helper' describe "Router Basic" do before :all do class ::BlogsController include Rad::CoreRoutingHelper def show; end end end after :all do remove_constants %w(BlogsController) end before :each do @router = Rad::Router.new :class @params = Rad::Params.new @router.stub(:safe_workspace).and_return({params: @params}.to_openobject) end it "encode" do @router.encode(BlogsController, :show, format: 'json').should == ["/blogs_controller/show.json", {}] end it "encode_method" do lambda{@router.encode_method :class_method}.should raise_error(/route method/) @router.routes.first.should_receive(:encode_method).and_return([BlogsController, :show]) @router.encode_method(:class_method).should == [BlogsController, :show] end it "decode" do @router.decode("/blogs_controller/show", {}).should == [BlogsController, :show, {}] end it "skip" do @router.skip(/\/fs/) skip = catch :skip do @router.decode("/fs/user/avatar", {}) nil end skip.should_not be_nil end describe "url_for" do it "should convert complex params to json" do @router.url_for(BlogsController, :show, a: :b, as_json: true).should == %(/blogs_controller/show?json=%7B%22a%22%3A%22b%22%7D) end it "should escape params" do @router.url_for(BlogsController, :show, a: 'b/c').should == "/blogs_controller/show?a=b%2Fc" end it "url_for" do @router.stub(:current_class).and_return(BlogsController) @router.url_for(BlogsController, :show, format: 'json').should == "/blogs_controller/show.json" @router.url_for(:show, format: 'json').should == "/blogs_controller/show.json" @router.url_for(BlogsController, :show).should == "/blogs_controller/show" @router.url_for(:show).should == "/blogs_controller/show" @router.url_for(:show).should == "/blogs_controller/show" 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(BlogsController) @router.url_for(:show, format: 'js').marks.format.should == 'js' @router.url_for('/path').marks.format.should be_nil @router.url_for('/path', format: 'js').marks.format.should == 'js' end it "shouldn't modify arguments (from error)" do options = {format: :js} @router.url_for(BlogsController, :show, options).should == "/blogs_controller/show.js" options.should == {format: :js} end end describe "persistent params" do before :each do @router.persistent_params << :l end it "global persistent params" do @params.l, @params.id = 'ru', 1 @router.url_for(BlogsController, :show).should == "/blogs_controller/show?l=ru" @router.url_for(BlogsController, :show, l: 'en').should == "/blogs_controller/show?l=en" end it "should persist params with begining underscore" do @params._space, @params.id = 'space_id', 1 @router.url_for(BlogsController, :show).should == '/blogs_controller/show' @router.persist_params do @router.url_for(BlogsController, :show).should == '/blogs_controller/show?_space=space_id' @router.url_for(BlogsController, :show, _space: 'another_id').should == '/blogs_controller/show?_space=another_id' end end end end