Skip to content

Commit

Permalink
chore: load runtime error problem+json decorator from configuration o…
Browse files Browse the repository at this point in the history
…bject
  • Loading branch information
bethesque committed Nov 22, 2023
1 parent ed14236 commit 7b0033e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module PactBroker
module Api
module Decorators
class RuntimeErrorProblemJSONDecorator
class RuntimeErrorProblemJsonDecorator

# @param message [String]
def initialize(message)
Expand Down
6 changes: 5 additions & 1 deletion lib/pact_broker/api/resources/error_response_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def self.display_message(error, message, obfuscated_message)
end

private_class_method def self.problem_json_response_body(message, env)
PactBroker::Api::Decorators::RuntimeErrorProblemJSONDecorator.new(message).to_hash(user_options: { base_url: env["pactbroker.base_url" ] })
error_decorator_class(env).new(message).to_hash(user_options: { base_url: env["pactbroker.base_url" ] })
end

private_class_method def self.obfuscated_error_message(error_reference)
Expand All @@ -76,6 +76,10 @@ def self.display_message(error, message, obfuscated_message)
private_class_method def self.problem_json?(env)
env["HTTP_ACCEPT"]&.include?("application/problem+json")
end

private_class_method def self.error_decorator_class(env)
env["pactbroker.application_context"].decorator_configuration.class_for(:runtime_error_problem_json_decorator)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require "pact_broker/api/resources/error_response_generator"
require "pact_broker/application_context"
require "pact_broker/api/decorators/runtime_error_problem_json_decorator"

module PactBroker
module Api
Expand All @@ -13,7 +15,7 @@ module Resources
let(:error_reference) { "bYWfnyWPlf" }

let(:headers_and_body) { ErrorResponseGenerator.call(error, error_reference, rack_env) }
let(:rack_env) { { "pactbroker.base_url" => "http://example.org" } }
let(:rack_env) { { "pactbroker.base_url" => "http://example.org", "pactbroker.application_context" => PactBroker::ApplicationContext.default_application_context } }
let(:headers) { headers_and_body.first }

subject { JSON.parse(headers_and_body.last) }
Expand All @@ -35,7 +37,7 @@ module Resources
end

context "when the Accept header includes application/problem+json" do
let(:rack_env) { { "HTTP_ACCEPT" => "application/hal+json, application/problem+json", "pactbroker.base_url" => "http://example.org" } }
let(:rack_env) { { "HTTP_ACCEPT" => "application/hal+json, application/problem+json", "pactbroker.base_url" => "http://example.org", "pactbroker.application_context" => PactBroker::ApplicationContext.default_application_context } }

it "returns headers" do
expect(headers).to eq("Content-Type" => "application/problem+json;charset=utf-8")
Expand Down

0 comments on commit 7b0033e

Please sign in to comment.