Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OPTIONS regression #582

Merged
merged 2 commits into from
Feb 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -561,31 +561,29 @@ def cascade?
# cannot handle.
def add_head_not_allowed_methods_and_options_methods
allowed_methods = Hash.new { |h, k| h[k] = [] }
resources = self.class.endpoints.map do |endpoint|
if endpoint.options[:app] && endpoint.options[:app].respond_to?(:endpoints)
endpoint.options[:app].endpoints.map(&:routes)
else
endpoint.routes
self.class.endpoints.each do |endpoint|
app = endpoint.endpoints ? endpoint.options[:app] : self.class
(endpoint.endpoints || [endpoint]).each do |e|
e.options[:path].each do |path|
allowed_methods[[app, path]] |= e.options[:method]
end
end
end
resources.flatten.each do |route|
allowed_methods[route.route_path] << route.route_method
end
allowed_methods.each do |path, methods|
allowed_methods.each do |(app, path), methods|
if methods.include?('GET') && !methods.include?('HEAD') && !self.class.settings[:do_not_route_head]
methods = methods | ['HEAD']
end
allow_header = (['OPTIONS'] | methods).join(', ')
if methods.include?('OPTIONS') || !self.class.settings[:do_not_route_options]
self.class.options(path, {}) do
if !methods.include?('OPTIONS') && !self.class.settings[:do_not_route_options]
app.options(path, {}) do
header 'Allow', allow_header
status 204
''
end
end
not_allowed_methods = %w(GET PUT POST DELETE PATCH HEAD) - methods
not_allowed_methods << 'OPTIONS' if self.class.settings[:do_not_route_options]
self.class.route(not_allowed_methods, path) do
app.route(not_allowed_methods, path) do
header 'Allow', allow_header
status 405
''
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,6 @@ def route
env["rack.routing_args"][:route_info]
end

protected

# Return the collection of endpoints within this endpoint.
# This is the case when an Grape::API mounts another Grape::API.
def endpoints
Expand All @@ -374,6 +372,8 @@ def endpoints
end
end

protected

def run(env)
@env = env
@header = {}
Expand Down
32 changes: 32 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2101,6 +2101,38 @@ def self.call(object, env)
last_response.body.should == "play"
end

it 'responds to options' do
subject.namespace :apples do
app = Class.new(Grape::API)
app.get('/colour') do
"red"
end
mount app
end
get '/apples/colour'
last_response.status.should eql 200
last_response.body.should == 'red'
options '/apples/colour'
last_response.status.should eql 204
end

it 'responds to options with versioning' do
subject.version 'v1', using: :path
subject.namespace :apples do
app = Class.new(Grape::API)
app.get('/colour') do
"red"
end
mount app
end

get '/v1/apples/colour'
last_response.status.should eql 200
last_response.body.should == 'red'
options '/v1/apples/colour'
last_response.status.should eql 204
end

end
end

Expand Down