Testing Parameter Modification Behavior in Grape API Endpoints
This test suite validates parameter modification behavior in Grape API endpoints, focusing on maintaining parameter default values across multiple requests. It ensures that modifying parameter values during request processing doesn’t affect subsequent requests.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
ruby-grape/grape
spec/grape/api/parameters_modification_spec.rb
# frozen_string_literal: true
describe Grape::Endpoint do
subject { Class.new(Grape::API) }
def app
subject
end
before do
subject.namespace :test do
params do
optional :foo, default: +'-abcdef'
end
get do
params[:foo].slice!(0)
params[:foo]
end
end
end
context 'when route modifies param value' do
it 'param default should not change' do
get '/test'
expect(last_response.status).to eq 200
expect(last_response.body).to eq 'abcdef'
get '/test'
expect(last_response.status).to eq 200
expect(last_response.body).to eq 'abcdef'
get '/test?foo=-123456'
expect(last_response.status).to eq 200
expect(last_response.body).to eq '123456'
get '/test'
expect(last_response.status).to eq 200
expect(last_response.body).to eq 'abcdef'
end
end
end