Skip to content

Commit

Permalink
fix(#5): Prevent to change the response if the endpoint returns a hash
Browse files Browse the repository at this point in the history
  • Loading branch information
jcagarcia committed Nov 6, 2023
1 parent cdfbfad commit de44586
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All changes to `grape-idempotency` will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.2] - 2023-01-06

### Fix

- Return correct original response when the endpoint returns a hash in the body


## [0.1.1] - 2023-01-06

### Fix
Expand Down
10 changes: 6 additions & 4 deletions lib/grape/idempotency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ def idempotent(grape, &block)
block.call
end

if response.is_a?(Hash)
response = response[:message].to_json
end
response = response[:message].to_json if is_an_error?(response)

original_request_id = get_request_id(grape.request.headers)
grape.header(ORIGINAL_REQUEST_HEADER, original_request_id)
response
grape.body response
ensure
validate_config!
unless cached_request
Expand Down Expand Up @@ -107,6 +105,10 @@ def store_in_cache(idempotency_key, path, params, status, request_id, response)
end
end

def is_an_error?(response)
response.is_a?(Hash) && response.has_key?(:message) && response.has_key?(:headers) && response.has_key?(:status)
end

def key(idempotency_key)
"grape:idempotency:#{idempotency_key}"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/idempotency/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Grape
module Idempotency
VERSION = '0.1.1'
VERSION = '0.1.2'
end
end
23 changes: 23 additions & 0 deletions spec/idempotent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,29 @@
expect(last_response.headers).to include("original-request" => "req_123456")
end
end

context 'when the endpoint returns a hash' do
it 'returns the original response' do
allow(SecureRandom).to receive(:random_number).and_return(1, 2)

app.post('/payments') do
idempotent do
status 200
{ amount_to: SecureRandom.random_number }
end
end

header "idempotency-key", idempotency_key
post 'payments?locale=es', { amount: 100_00 }.to_json
expect(last_response.status).to eq(200)
expect(last_response.body).to eq("{:amount_to=>1}")

header "idempotency-key", idempotency_key
post 'payments?locale=es', { amount: 100_00 }.to_json
expect(last_response.status).to eq(200)
expect(last_response.body).to eq("{\"amount_to\"=>1}")
end
end
end

context 'but any of the provided parameters does NOT match with the original request' do
Expand Down

0 comments on commit de44586

Please sign in to comment.