Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix OPTIONS regression. fixes #573

commit 65a3056f71598a7f168fb8033418853a243e8fe7 1 parent e300202
@stevschmid stevschmid authored
Showing with 45 additions and 14 deletions.
  1. +10 −12 lib/grape/api.rb
  2. +2 −2 lib/grape/endpoint.rb
  3. +33 −0 spec/grape/api_spec.rb
View
22 lib/grape/api.rb
@@ -561,23 +561,21 @@ 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
''
@@ -585,7 +583,7 @@ def add_head_not_allowed_methods_and_options_methods
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
''
View
4 lib/grape/endpoint.rb
@@ -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
@@ -374,6 +372,8 @@ def endpoints
end
end
+ protected
+
def run(env)
@env = env
@header = {}
View
33 spec/grape/api_spec.rb
@@ -2101,6 +2101,39 @@ 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
Please sign in to comment.
Something went wrong with that request. Please try again.