require 'router/spec_helper' describe "AliasRouter" do before :all do class SomeController end end after :all do remove_constants %w(SomeController) end before :each do @router = Rad::AliasRouter.new end it "encode / decode" do @router.add '/login', class: SomeController, method: :login @router.encode(SomeController, :login, {}).should == ["/login", {}] @router.encode_method('login_path').should == [SomeController, :login] @router.decode("/login", {}).should == [SomeController, :login, {}] end it "should differentiate aliases with similar path elements" do @router.add '/login', class: SomeController, method: :login @router.add '/login/twitter', class: SomeController, method: :twitter @router.encode(SomeController, :login, {}).should == ["/login", {}] @router.decode("/login", {}).should == [SomeController, :login, {}] @router.encode(SomeController, :twitter, {}).should == ["/login/twitter", {}] @router.decode("/login/twitter", {}).should == [SomeController, :twitter, {}] end it "input validation" do lambda{@router.add :login, class: SomeController, method: :login}.should raise_error(/\//) lambda{@router.add '/login', class: SomeController}.should raise_error(/method/) lambda{@router.add '/login', method: :login}.should raise_error(/class/) lambda{@router.add '/login', class: SomeController, method: :login, invalid: 'value'}.should raise_error(/unknown/) lambda{@router.add '/login', class: SomeController, method: :login, url_root: 'users'}.should raise_error(/\//) lambda{@router.add '/login', class: SomeController, method: :login, prefix: []}.should raise_error(/empty/) end it "should use :url_root" do @router.add '/login', class: SomeController, method: :login, url_root: '/space' @router.encode(SomeController, :login, {}).should == ["/space/login", {}] @router.decode("/space/login", {}).should == [SomeController, :login, {}] end it "should return nil if route unknown" do @router.encode(SomeController, :unknown, {}).should be_nil @router.encode_method(:unknown_path).should be_nil @router.decode("/login", {}).should be_nil end it 'root alias' do @router.add '/', class: SomeController, method: :home @router.encode(SomeController, :home, {}).should == ["/", {}] @router.decode("/", {}).should == [SomeController, :home, {}] # should not support url_root -> {@router.add '/', class: SomeController, method: :home, url_root: '/space'}.should raise_error(/url_root/) end describe "prefixes" do it "basics" do @router.add '/statistics', class: SomeController, method: :statistics, prefix: [:l, :space] @router.encode(SomeController, :statistics, l: 'en', space: 'personal').should == ["/l/en/space/personal/statistics", {}] @router.encode_method("statistics_path").should == [SomeController, :statistics] @router.decode("/l/en/space/personal/statistics", {}).should == [SomeController, :statistics, {l: 'en', space: 'personal'}] end it "should also works with path with slashes" do @router.add '/blogs/show', class: SomeController, method: :show, prefix: [:l, :space] @router.encode(SomeController, :show, l: 'en', space: 'personal').should == ["/l/en/space/personal/blogs/show", {}] @router.decode("/l/en/space/personal/blogs/show", {}).should == [SomeController, :show, {l: 'en', space: 'personal'}] end it "should raise error if prefixes not provided" do @router.add '/login', class: SomeController, method: :login, prefix: [:l] lambda{@router.encode(SomeController, :login, {})}.should raise_error(/prefix/) lambda{@router.decode('/login', {})}.should raise_error(/prefix/) end it "should works with :url_root" do @router.add '/blogs/show', class: SomeController, method: :show, prefix: [:l], url_root: '/users' @router.encode(SomeController, :show, l: 'en').should == ["/users/l/en/blogs/show", {}] @router.decode("/users/l/en/blogs/show", {}).should == [SomeController, :show, {l: 'en'}] end end end