Permalink
Browse files

Issue #24: Strip leading slash from :path if present

  • Loading branch information...
1 parent 57ca17f commit 147659328a1624ea33f60d40653c65db98a375d6 Brian Ploetz committed Jul 12, 2012
Showing with 26 additions and 3 deletions.
  1. +1 −1 README.markdown
  2. +1 −0 lib/versionist/routing.rb
  3. +24 −2 spec/api_routing_spec.rb
View
@@ -89,7 +89,7 @@ Example:
```ruby
MyApi::Application.routes.draw do
- api_version(:module => "V3", :path => "/v3") do
+ api_version(:module => "V3", :path => "v3") do
match '/foos.(:format)' => 'foos#index', :via => :get
match '/foos_no_format' => 'foos#index', :via => :get
resources :bars
@@ -36,6 +36,7 @@ def configure_path(config, &block)
path = Versionist::VersioningStrategy::Path.new(config)
# Use the :as option and strip out non-word characters from the path to avoid this:
# https://github.com/rails/rails/issues/3224
+ config[:path].slice!(0) if config[:path] =~ /^\//
route_hash = {:module => config[:module], :as => config[:path].gsub(/\W/, '_')}
route_hash.merge!({:defaults => config[:defaults]}) if config.has_key?(:defaults)
namespace(config[:path], route_hash, &block)
@@ -438,10 +438,20 @@
assert_equal 'application/json', response.content_type
assert_equal ver, response.body
+ get "/#{ver}/bars.json", nil, @headers
+ assert_response 200
+ assert_equal 'application/json', response.content_type
+ assert_equal ver, response.body
+
get "/#{ver}/foos.xml", nil, @headers
assert_response 200
assert_equal 'application/xml', response.content_type
assert_equal ver, response.body
+
+ get "/#{ver}/bars.xml", nil, @headers
+ assert_response 200
+ assert_equal 'application/xml', response.content_type
+ assert_equal ver, response.body
end
it "should route to the correct controller when path matches and format specified via accept header" do
@@ -463,9 +473,11 @@
TestApi::Application.routes.draw do
api_version({:module => mod, :path => "/#{ver}", :default => true}) do
match '/foos.(:format)' => 'foos#index', :via => :get
+ resources :bars
end
api_version({:module => "not_default", :path => "/not_default"}) do
match '/foos.(:format)' => 'foos#index', :via => :get
+ resources :bars
end
end
end
@@ -476,10 +488,20 @@
assert_equal 'application/json', response.content_type
assert_equal ver, response.body
+ get "/#{ver}/bars.json", nil, @headers
+ assert_response 200
+ assert_equal 'application/json', response.content_type
+ assert_equal ver, response.body
+
get "/foos.xml", nil, @headers
assert_response 200
assert_equal 'application/xml', response.content_type
assert_equal ver, response.body
+
+ get "/#{ver}/bars.xml", nil, @headers
+ assert_response 200
+ assert_equal 'application/xml', response.content_type
+ assert_equal ver, response.body
end
it "should not route to the default when another configured version is given" do
@@ -492,7 +514,7 @@
context ":defaults" do
it "should pass the :defaults hash on to the namespace() call" do
- ActionDispatch::Routing::Mapper.any_instance.should_receive(:namespace).with("/#{ver}", hash_including(:defaults => {:format => :json}))
+ ActionDispatch::Routing::Mapper.any_instance.should_receive(:namespace).with("#{ver}", hash_including(:defaults => {:format => :json}))
TestApi::Application.routes.draw do
api_version({:module => mod, :path => "/#{ver}", :defaults => {:format => :json}}) do
match '/foos.(:format)' => 'foos#index', :via => :get
@@ -503,7 +525,7 @@
end
it "should pass the :defaults hash on to the namespace() call and the scope() call when :default is present" do
- ActionDispatch::Routing::Mapper.any_instance.should_receive(:namespace).with("/#{ver}", hash_including(:defaults => {:format => :json}))
+ ActionDispatch::Routing::Mapper.any_instance.should_receive(:namespace).with("#{ver}", hash_including(:defaults => {:format => :json}))
ActionDispatch::Routing::Mapper.any_instance.should_receive(:scope).with(hash_including(:defaults => {:format => :json}))
TestApi::Application.routes.draw do
api_version({:module => mod, :path => "/#{ver}", :default => true, :defaults => {:format => :json}}) do

0 comments on commit 1476593

Please sign in to comment.