diff --git a/.rubocop.yml b/.rubocop.yml index a5b51f7b..123ed0c4 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -2,3 +2,7 @@ inherit_from: .rubocop_todo.yml Style/StringLiterals: Enabled: false + +Metrics/BlockLength: + Exclude: + - 'spec/**/*' diff --git a/lib/rack/sendgrid_webhook_verification.rb b/lib/rack/sendgrid_webhook_verification.rb index cf952d60..280e2bd9 100644 --- a/lib/rack/sendgrid_webhook_verification.rb +++ b/lib/rack/sendgrid_webhook_verification.rb @@ -39,6 +39,8 @@ def call(env) request.env[SendGrid::EventWebhookHeader::TIMESTAMP] ) + request.body.rewind + if verified @app.call(env) else diff --git a/spec/rack/sendgrid_webhook_verification_spec.rb b/spec/rack/sendgrid_webhook_verification_spec.rb index 11bfd0b1..c8d2ee2c 100644 --- a/spec/rack/sendgrid_webhook_verification_spec.rb +++ b/spec/rack/sendgrid_webhook_verification_spec.rb @@ -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