require 'spec_helper' describe "RestfulRouter" do before :all do class ::BlogController def show; end end end after :all do remove_constants %w(BlogController) end before do @router = Rad::Router::RestfulRouter.new end describe 'core API' do it "class resource" do @router.add :blogs, class_name: 'BlogController' @router.encode(BlogController, :show, {}).should == ["/blogs/show", {}] @router.encode_method('show_blog_path', false).should == [BlogController, :show] @router.encode_method('show_blogs_path', false).should == [BlogController, :show] @router.decode("/blogs/show", {}).should == [BlogController, :show, {}] end it "object resource" do @router.add :blogs, class_name: 'BlogController' @router.encode(BlogController, :show, id: '10', view: 'full').should == ["/blogs/10/show", {view: 'full'}] @router.encode_method('show_blog_path', true).should == [BlogController, :show] @router.encode_method('show_blogs_path', true).should == [BlogController, :show] @router.decode("/blogs/10/show", {view: 'full'}).should == [BlogController, :show, {id: '10', view: 'full'}] end end describe "prefixes" do it "class resource" do @router.add :blogs, class_name: 'BlogController', prefix: [:l, :space] @router.encode(BlogController, :show, l: 'en', space: 'personal').should == ["/en/personal/blogs/show", {}] @router.decode("/en/personal/blogs/show", {}).should == [BlogController, :show, {l: 'en', space: 'personal'}] end it "object resource" do @router.add :blogs, class_name: 'BlogController', prefix: [:l, :space] @router.encode(BlogController, :show, id: '10', l: 'en', space: 'personal').should == ["/en/personal/blogs/10/show", {}] @router.decode("/en/personal/blogs/10/show", {}).should == [BlogController, :show, {id: '10', l: 'en', space: 'personal'}] end it "should raise error or return nil if prefixes not provided" do @router.add :blogs, class_name: 'BlogController', prefix: :l lambda{@router.encode(BlogController, :show, {})}.should raise_error(/prefix/) @router.decode('/blogs/show', {}).should be_nil lambda{@router.encode(BlogController, :show, id: '10')}.should raise_error(/prefix/) @router.decode('/blogs/10/show', {}).should be_nil end it "should works with :url_root" do @router.add :blogs, class_name: 'BlogController', prefix: :l, url_root: '/users' @router.encode(BlogController, :show, l: 'en').should == ["/en/blogs/show", {url_root: '/users'}] @router.decode("/users/en/blogs/show", {}).should == [BlogController, :show, {l: 'en'}] @router.encode(BlogController, :show, id: '10', l: 'en').should == ["/en/blogs/10/show", {url_root: '/users'}] @router.decode("/users/en/blogs/10/show", {}).should == [BlogController, :show, {id: '10', l: 'en'}] end it "should be able to use alias for prefix parameter" do @router.add :blogs, class_name: 'BlogController', prefix: [:l, :space_id] @router.encode(BlogController, :show, l: 'en', space_id: 'personal').should == ["/en/personal/blogs/show", {}] @router.decode("/en/personal/blogs/show", {}).should == [BlogController, :show, {l: 'en', space_id: 'personal'}] end end describe "micelaneous check" do it "should allow only plural form in resource definition" do lambda{@router.add :blog, class_name: 'BlogController'}.should raise_error(/plural/) @router.add :blogs, class_name: 'BlogController' end it "shouldn't allow slashes in resource name" do lambda{@router.add '/admin/blogs', class_name: 'BlogController'}.should raise_error(/\//) end it "should correctly works with unknown routes" do @router.encode(BlogController, :show, {}).should be_nil @router.encode_method('show_blog_path', false).should be_nil @router.encode_method('show_blogs_path', false).should be_nil @router.decode("/blogs/show", {}).should be_nil @router.encode(BlogController, :show, id: '10', view: 'full').should be_nil @router.encode_method('show_blog_path', true).should be_nil @router.encode_method('show_blogs_path', true).should be_nil @router.decode("/blogs/10/show", view: 'full').should be_nil end end describe ":url_root" do it "should use :url_root" do @router.add :blogs, class_name: 'BlogController', url_root: '/users' @router.encode(BlogController, :show, {}).should == ["/blogs/show", {url_root: '/users'}] @router.decode("/users/blogs/show", {}).should == [BlogController, :show, {}] @router.encode(BlogController, :show, id: '10').should == ["/blogs/10/show", {url_root: '/users'}] @router.decode("/users/blogs/10/show", {}).should == [BlogController, :show, {id: '10'}] end it "should ignore slash" do @router.add :blogs, class_name: 'BlogController', url_root: '/' @router.encode(BlogController, :show, {}).should == ["/blogs/show", {}] @router.decode("/blogs/show", {}).should == [BlogController, :show, {}] end end it "input validation" do lambda{@router.add :blogs, class_name: 'BlogController', invalid: 'value'}.should raise_error(/unknown/) lambda{@router.add :blogs}.should raise_error(/class/) lambda{@router.add :blogs, class_name: 'BlogController', url_root: 'users'}.should raise_error(/\//) end end