Skip to content

Commit

Permalink
Merge pull request #582 from stevschmid/573-fix
Browse files Browse the repository at this point in the history
Fix OPTIONS regression
  • Loading branch information
dblock committed Feb 21, 2014
2 parents e300202 + be69b1b commit f7ce50d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
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

0 comments on commit f7ce50d

Please sign in to comment.