Skip to content

Commit

Permalink
Merge pull request #853 from dblock/fix-679
Browse files Browse the repository at this point in the history
Fix #679: OPTIONS and unsupported methods returning 404 when combined with prefix.
  • Loading branch information
dblock committed Dec 16, 2014
2 parents 49c47e0 + 3c65c82 commit 847f65c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* [#826](https://github.com/intridea/grape/pull/826): Find `coerce_type` for `Array` when not specified - [@manovotn](https://github.com/manovotn).
* [#645](https://github.com/intridea/grape/issues/645): Invoking `body false` will return `204 No Content` - [@dblock](https://github.com/dblock).
* [#801](https://github.com/intridea/grape/issues/801): Evaluate permitted parameter `values` lazily on each request when declared as a proc - [@dblock](https://github.com/dblock).
* [#679](https://github.com/intridea/grape/issues/679): Fixed `OPTIONS` method returning 404 when combined with `prefix`- [@dblock](https://github.com/dblock).
* [#679](https://github.com/intridea/grape/issues/679): Fixed unsupported methods returning 404 instead of 405 when combined with `prefix`- [@dblock](https://github.com/dblock).
* Your contribution here.

0.9.0 (8/27/2014)
Expand Down
52 changes: 32 additions & 20 deletions lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,30 +141,32 @@ def add_head_not_allowed_methods_and_options_methods
# contain already versioning information when using path versioning.
# Disable versioning so adding a route won't prepend versioning
# informations again.
without_versioning do
methods_per_path.each do |path, methods|
allowed_methods = methods.dup
unless self.class.namespace_inheritable(:do_not_route_head)
allowed_methods |= ['HEAD'] if allowed_methods.include?('GET')
end
without_root_prefix do
without_versioning do
methods_per_path.each do |path, methods|
allowed_methods = methods.dup
unless self.class.namespace_inheritable(:do_not_route_head)
allowed_methods |= ['HEAD'] if allowed_methods.include?('GET')
end

allow_header = (['OPTIONS'] | allowed_methods).join(', ')
unless self.class.namespace_inheritable(:do_not_route_options)
unless allowed_methods.include?('OPTIONS')
self.class.options(path, {}) do
header 'Allow', allow_header
status 204
''
allow_header = (['OPTIONS'] | allowed_methods).join(', ')
unless self.class.namespace_inheritable(:do_not_route_options)
unless allowed_methods.include?('OPTIONS')
self.class.options(path, {}) do
header 'Allow', allow_header
status 204
''
end
end
end
end

not_allowed_methods = %w(GET PUT POST DELETE PATCH HEAD) - allowed_methods
not_allowed_methods << 'OPTIONS' if self.class.namespace_inheritable(:do_not_route_options)
self.class.route(not_allowed_methods, path) do
header 'Allow', allow_header
status 405
''
not_allowed_methods = %w(GET PUT POST DELETE PATCH HEAD) - allowed_methods
not_allowed_methods << 'OPTIONS' if self.class.namespace_inheritable(:do_not_route_options)
self.class.route(not_allowed_methods, path) do
header 'Allow', allow_header
status 405
''
end
end
end
end
Expand All @@ -182,5 +184,15 @@ def without_versioning(&block)
self.class.namespace_inheritable(:version, old_version)
self.class.namespace_inheritable(:version_options, old_version_options)
end

def without_root_prefix(&block)
old_prefix = self.class.namespace_inheritable(:root_prefix)

self.class.namespace_inheritable_to_nil(:root_prefix)

yield

self.class.namespace_inheritable(:root_prefix, old_prefix)
end
end
end
20 changes: 20 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def app
end

get 'awesome/sauce/'
expect(last_response.status).to eql 200
expect(last_response.body).to eql 'Hello there.'
end

Expand All @@ -32,6 +33,25 @@ def app
get '/hello'
expect(last_response.status).to eql 404
end

it 'supports OPTIONS' do
subject.prefix 'awesome/sauce'
subject.get do
'Hello there.'
end

options 'awesome/sauce'
expect(last_response.status).to eql 204
expect(last_response.body).to be_blank
end

it 'disallows POST' do
subject.prefix 'awesome/sauce'
subject.get

post 'awesome/sauce'
expect(last_response.status).to eql 405
end
end

describe '.version' do
Expand Down

0 comments on commit 847f65c

Please sign in to comment.