Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure the request body is not emptied in Faraday v1.0.0 #43

Merged
merged 2 commits into from Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/jwt_signed_request/middlewares/faraday.rb
Expand Up @@ -12,11 +12,13 @@ def initialize(app, options)
end

def call(env)
env[:body] ||= ::JWTSignedRequest::EMPTY_BODY

@jwt_token = ::JWTSignedRequest.sign(
method: env[:method],
path: env[:url].request_uri,
headers: env[:request_headers],
body: env.fetch(:body, ::JWTSignedRequest::EMPTY_BODY),
body: env[:body],
**optional_settings
)

Expand Down
9 changes: 7 additions & 2 deletions spec/jwt_signed_request/middlewares/faraday_spec.rb
Expand Up @@ -11,14 +11,14 @@
end

let(:env) do
{
Faraday::Env.from(
method: 'POST',
url: double(:request_uri => '/api/endpoint?offset=1&limit=10'),
body: 'body',
request_headers: {
'Content-Type' => 'application/json'
}
}
)
end

let(:jwt_token) { SecureRandom.hex }
Expand Down Expand Up @@ -54,6 +54,11 @@
expect(response[:request_headers]).to include('Authorization' => jwt_token)
end

it 'does not empty request body' do
request_env = middleware.call(env).env
expect(request_env[:body]).to eq('body')
end

context 'when bearer_schema is requested' do
let(:options) do
{
Expand Down