From 25f8ec0fce3b10e158a931bd841fa753b1349b63 Mon Sep 17 00:00:00 2001 From: dblock Date: Sat, 5 Jan 2013 14:35:40 -0500 Subject: [PATCH] Fix #196: root requests don't work with prefix. --- CHANGELOG.markdown | 2 +- lib/grape/endpoint.rb | 5 +++-- spec/grape/api_spec.rb | 12 ++++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index 5507ef8e31..ab90c189a2 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -15,7 +15,7 @@ * [#133](https://github.com/intridea/grape/issues/133): The value of `env['PATH_INFO']` is no longer altered with `path` versioning - [@dblock](https://github.com/dblock). * [#280](https://github.com/intridea/grape/issues/280): Fix: grouped parameters mangled in `route_params` hash - [@marcusg](https://github.com/marcusg), [@dblock](https://github.com/dblock). * [#304](https://github.com/intridea/grape/issues/304): Fix: `present x, :with => Entity` returns class references with `format :json` - [@dblock](https://github.com/dblock). - +* [#196](https://github.com/intridea/grape/issues/196): Fix: root requests don't work with `prefix` - [@dblock](https://github.com/dblock). * Your contribution here. 0.2.3 (24/12/2012) diff --git a/lib/grape/endpoint.rb b/lib/grape/endpoint.rb index 920ec17396..d2d65d00c8 100644 --- a/lib/grape/endpoint.rb +++ b/lib/grape/endpoint.rb @@ -110,7 +110,7 @@ def prepare_routes def prepare_path(path) parts = [] - parts << settings[:root_prefix] if settings[:root_prefix] + parts << settings[:root_prefix].to_s.split("/") if settings[:root_prefix] uses_path_versioning = settings[:version] && settings[:version_options][:using] == :path namespace_is_empty = namespace && (namespace.to_s =~ /^\s*$/ || namespace.to_s == '/') @@ -119,11 +119,12 @@ def prepare_path(path) parts << ':version' if uses_path_versioning if !uses_path_versioning || (!namespace_is_empty || !path_is_empty) parts << namespace.to_s if namespace - parts << path.to_s if path && '/' != path + parts << path.to_s if path format_suffix = '(.:format)' else format_suffix = '(/.:format)' end + parts = parts.flatten.select { |part| part != '/' } Rack::Mount::Utils.normalize_path(parts.join('/') + format_suffix) end diff --git a/spec/grape/api_spec.rb b/spec/grape/api_spec.rb index 82f98c06da..532c09b08c 100644 --- a/spec/grape/api_spec.rb +++ b/spec/grape/api_spec.rb @@ -7,6 +7,17 @@ def app; subject end describe '.prefix' do + + it 'routes root through with the prefix' do + subject.prefix 'awesome/sauce' + subject.get do + "Hello there." + end + + get 'awesome/sauce/' + last_response.body.should eql "Hello there." + end + it 'routes through with the prefix' do subject.prefix 'awesome/sauce' subject.get :hello do @@ -19,6 +30,7 @@ def app; subject end get '/hello' last_response.status.should eql 404 end + end describe '.version' do