Skip to content

Commit

Permalink
fix: reading position of a request body (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
snaka authored Sep 29, 2020
1 parent a8ef12f commit 969ef94
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ inherit_from: .rubocop_todo.yml

Style/StringLiterals:
Enabled: false

Metrics/BlockLength:
Exclude:
- 'spec/**/*'
2 changes: 2 additions & 0 deletions lib/rack/sendgrid_webhook_verification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def call(env)
request.env[SendGrid::EventWebhookHeader::TIMESTAMP]
)

request.body.rewind

if verified
@app.call(env)
else
Expand Down
26 changes: 26 additions & 0 deletions spec/rack/sendgrid_webhook_verification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,31 @@
expect(status).to eq(403)
end
end

describe 'request body which passed to an app' do
before do
@payload = nil
@spy_app = lambda do |env|
@payload = Rack::Request.new(env).body
[200, { 'Content-Type' => 'text/plain' }, ['Hello']]
end
end

let(:middleware) { Rack::SendGridWebhookVerification.new(@spy_app, public_key, %r{/email}) }

it 'keeps orignal reading position' do
options = {
:input => Fixtures::EventWebhook::PAYLOAD,
'Content-Type' => "application/json"
}
options[SendGrid::EventWebhookHeader::SIGNATURE] = Fixtures::EventWebhook::SIGNATURE
options[SendGrid::EventWebhookHeader::TIMESTAMP] = Fixtures::EventWebhook::TIMESTAMP
request = Rack::MockRequest.env_for('/email', options)
status, headers, body = middleware.call(request)
expect(status).to eq(200)
expect(@payload).not_to be_nil
expect(@payload.pos).to be_zero
end
end
end
end

0 comments on commit 969ef94

Please sign in to comment.