Skip to content

Instantly share code, notes, and snippets.

@crash2burn
Created September 11, 2008 06:08
Show Gist options
  • Save crash2burn/10176 to your computer and use it in GitHub Desktop.
Save crash2burn/10176 to your computer and use it in GitHub Desktop.
## Note, this blows up even worse if you use something
## with /'s in it ie /blogposts/~~http://www.example.com~~
## it wont even recognize the route as it is now.
## Without /'s it currently appears to pass the regex as the id, not the matched text
describe "a resource route with a regex id" do
before :each do
Merb::Router.prepare do
with(:id => %r'~~(.*)~~') { resources :blogposts }
end
end
it "should have an index action with an optional :format" do
route_to('/blogposts').should have_route(:controller => 'blogposts', :action => 'index', :id => nil, :format => nil)
route_to('/blogposts/index').should have_route(:controller => 'blogposts', :action => 'index', :id => nil, :format => nil)
route_to('/blogposts.js').should have_route(:controller => 'blogposts', :action => 'index', :id => nil, :format => "js")
route_to('/blogposts/index.xml').should have_route(:controller => 'blogposts', :action => 'index', :id => nil, :format => "xml")
end
it "should have a create action with an optional :format" do
route_to('/blogposts', :method => :post).should have_route(:controller => 'blogposts', :action => 'create', :id => nil, :format => nil)
route_to('/blogposts.js', :method => :post).should have_route(:controller => 'blogposts', :action => 'create', :id => nil, :format => "js")
end
it "should not match put or delete on the collection" do
[:put, :delete].each do |method|
lambda { route_to('/blogposts', :method => method) }.should raise_not_found
lambda { route_to('/blogposts.js', :method => method) }.should raise_not_found
end
end
it "should have a new action with an optional :format" do
route_to('/blogposts/new', :method => :get).should have_route(:controller => 'blogposts', :action => 'new', :id => nil, :format => nil)
route_to('/blogposts/new.js', :method => :get).should have_route(:controller => 'blogposts', :action => 'new', :id => nil, :format => "js")
end
it "should not match post on the new action" do
lambda { route_to('/blogposts/new', :method => :post) }.should raise_not_found
lambda { route_to('/blogposts/new.xml', :method => :post) }.should raise_not_found
end
it "should have a show action with an optional :format" do
route_to('/blogposts/~~abc~~', :method => :get).should have_route(:controller => 'blogposts', :action => 'show', :id => "abc", :format => nil)
route_to('/blogposts/~~abc~~.css', :method => :get).should have_route(:controller => 'blogposts', :action => 'show', :id => "abc", :format => "css")
end
it "should have an update action with an optional :format" do
route_to('/blogposts/~~abc~~', :method => :put).should have_route(:controller => 'blogposts', :action => 'update', :id => "abc", :format => nil)
route_to('/blogposts/~~abc~~.csv', :method => :put).should have_route(:controller => 'blogposts', :action => 'update', :id => "abc", :format => "csv")
end
it "should have a destroy action with an optional :format" do
route_to('/blogposts/~~abc~~', :method => :delete).should have_route(:controller => 'blogposts', :action => 'destroy', :id => "abc", :format => nil)
route_to('/blogposts/~~abc~~.xxl', :method => :delete).should have_route(:controller => 'blogposts', :action => 'destroy', :id => "abc", :format => 'xxl')
end
it "should have an edit action with an optional :format" do
route_to('/blogposts/~~abc~~/edit', :method => :get).should have_route(:controller => 'blogposts', :action => 'edit', :id => "abc", :format => nil)
route_to('/blogposts/~~abc~~/edit.rss', :method => :get).should have_route(:controller => 'blogposts', :action => 'edit', :id => "abc", :format => "rss")
end
it "should not match post, put, or delete on the edit action" do
[:put, :post, :delete].each do |method|
lambda { route_to('/blogposts/edit', :method => :post) }.should raise_not_found
lambda { route_to('/blogposts/edit.hi', :method => :posts) }.should raise_not_found
end
end
it "should should have a delete action with an optional :format" do
route_to('/blogposts/~~abc~~/delete', :method => :get).should have_route(:controller => 'blogposts', :action => 'delete', :id => "1", :format => nil)
route_to('/blogposts/~~abc~~/delete.mp3', :method => :get).should have_route(:controller => 'blogposts', :action => 'delete', :id => "1", :format => "mp3")
end
it "should not match post, put, or delete on the delete action" do
[:put, :post, :delete].each do |method|
lambda { route_to('/blogposts/delete', :method => :post) }.should raise_not_found
lambda { route_to('/blogposts/delete.flv', :method => :post) }.should raise_not_found
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment