Skip to content

Commit

Permalink
Fix: regression from ruby-grape#1263, closes ruby-grape#1307.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock authored and przbadu committed Apr 5, 2016
1 parent c633040 commit 5f3c2c4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,14 @@ def add_head_not_allowed_methods_and_options_methods
self.class.endpoints.each do |endpoint|
routes = endpoint.routes
routes.each do |route|
methods_per_path[route.route_path] ||= []
methods_per_path[route.route_path] << route.route_method
# ignore any optional portions of the path
route_path = route.route_path.gsub(/\(.*\)/, '')

methods_per_path[route_path] ||= []
methods_per_path[route_path] << route.route_method

# using the :any shorthand produces [nil] for route methods, substitute all manually
methods_per_path[route.route_path] = %w(GET PUT POST DELETE PATCH HEAD OPTIONS) if methods_per_path[route.route_path].compact.empty?
methods_per_path[route_path] = %w(GET PUT POST DELETE PATCH HEAD OPTIONS) if methods_per_path[route_path].compact.empty?
end
end

Expand Down
43 changes: 43 additions & 0 deletions spec/grape/api/optional_parameters_in_route_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'spec_helper'

describe Grape::Endpoint do
subject { Class.new(Grape::API) }

def app
subject
end

before do
subject.namespace :api do
get ':id(/:ext)' do
[params[:id], params[:ext]].compact.join('/')
end

put ':id' do
params[:id]
end
end
end

context 'get' do
it 'responds without ext' do
get '/api/foo'
expect(last_response.status).to eq 200
expect(last_response.body).to eq 'foo'
end

it 'responds with ext' do
get '/api/foo/bar'
expect(last_response.status).to eq 200
expect(last_response.body).to eq 'foo/bar'
end
end

context 'put' do
it 'responds' do
put '/api/foo'
expect(last_response.status).to eq 200
expect(last_response.body).to eq 'foo'
end
end
end

0 comments on commit 5f3c2c4

Please sign in to comment.