diff --git a/lib/vault/client.rb b/lib/vault/client.rb index 8b25aa3..836aca3 100644 --- a/lib/vault/client.rb +++ b/lib/vault/client.rb @@ -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| @@ -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 @@ -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]) diff --git a/spec/unit/client_spec.rb b/spec/unit/client_spec.rb index b94460d..614f1d9 100644 --- a/spec/unit/client_spec.rb +++ b/spec/unit/client_spec.rb @@ -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 {