Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions lib/vault/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ class Client

# The default list of options to use when parsing JSON.
JSON_PARSE_OPTIONS = {
max_nesting: false,
create_additions: false,
symbolize_names: true,
max_nesting: false,
symbolize_names: true,
}.freeze

RESCUED_EXCEPTIONS = [].tap do |a|
Expand Down Expand Up @@ -385,7 +384,7 @@ def to_query_string(hash)
# the parsed response, as an object
def success(response)
if response.body && (response.content_type || '').include?("json")
JSON.parse(response.body, JSON_PARSE_OPTIONS)
JSON.parse(response.body, **JSON_PARSE_OPTIONS)
else
response.body
end
Expand Down Expand Up @@ -419,7 +418,7 @@ def error(response)
if (response.content_type || '').include?("json")
# Attempt to parse the error as JSON
begin
json = JSON.parse(response.body, JSON_PARSE_OPTIONS)
json = JSON.parse(response.body, **JSON_PARSE_OPTIONS)

if json[:errors]
raise klass.new(address, response, json[:errors])
Expand Down
22 changes: 22 additions & 0 deletions spec/unit/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@ module Vault
end
end

describe "JSON response parsing" do
before { subject.address = "https://vault.test" }

it "parses a JSON success response body with symbolized keys" do
stub_request(:get, "https://vault.test/")
.to_return(status: 200, body: '{"foo":"bar","nested":{"baz":1}}',
headers: { "Content-Type" => "application/json" })

expect(subject.get("/")).to eq(foo: "bar", nested: { baz: 1 })
end

it "raises an HTTPClientError with the errors parsed from a JSON error response body" do
stub_request(:get, "https://vault.test/")
.to_return(status: 400, body: '{"errors":["boom"]}',
headers: { "Content-Type" => "application/json" })

expect {
subject.get("/")
}.to raise_error(Vault::HTTPClientError) { |error| expect(error.errors).to eq(["boom"]) }
end
end

context "#with_retries" do
let(:options) do
{
Expand Down