Merge pull request #2665 from NobodysNightmare/fix/dots_in_string_objects

cover edge cases in string object matching
pull/2679/head
ulferts 10 years ago
commit 7e8909ee02
  1. 10
      lib/api/v3/string_objects/string_objects_api.rb
  2. 25
      spec/requests/api/v3/string_objects_resource_spec.rb

@ -33,13 +33,19 @@ module API
class StringObjectsAPI < Grape::API
resources :string_objects do
namespace ':value' do
params do
requires :value, type: String
end
# N.B. we are specifying our own requirements to allow arbitrary strings as value
# as of today values like '.foo' or 'foo.' would not be possible otherwise
route_param :value, requirements: { value: /.+/ } do
get do
StringObjectRepresenter.new(params[:value])
end
end
# answer requests for empty strings too
# we need to explicitly cover the empty case because of the way route matching works
get do
StringObjectRepresenter.new('')
end

@ -48,5 +48,30 @@ describe 'API v3 String Objects resource' do
it 'returns the value' do
expect(subject.body).to be_json_eql('foo bar'.to_json).at_path('value')
end
context 'empty string' do
let(:path) { '/api/v3/string_objects/' }
it 'is successful' do
expect(subject.status).to eql(200)
end
it 'returns the value' do
expect(subject.body).to be_json_eql(''.to_json).at_path('value')
end
end
# values starting with a dot is a known limitation of grapes default route matching
context 'beginning with .' do
let(:path) { '/api/v3/string_objects/.foo' }
it 'is successful' do
expect(subject.status).to eql(200)
end
it 'returns the value' do
expect(subject.body).to be_json_eql('.foo'.to_json).at_path('value')
end
end
end
end

Loading…
Cancel
Save