diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 46c06871a..40e22bce2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -54,8 +54,3 @@ config.filter_run_excluding skip: true config.include PactBroker::RackHelpers end - -if ENV["DEBUG"] == "true" - SemanticLogger.default_level = :info - SemanticLogger.add_appender(io: $stdout) -end diff --git a/spec/support/logging.rb b/spec/support/logging.rb index 38551529d..0522591e1 100644 --- a/spec/support/logging.rb +++ b/spec/support/logging.rb @@ -3,4 +3,28 @@ FileUtils.mkdir_p("log") SemanticLogger.default_level = :error -SemanticLogger.add_appender(file_name: "log/test.log", formatter: PactBroker::Logging::DefaultFormatter.new) + +if ENV["DEBUG"] == "true" + SemanticLogger.default_level = :info + SemanticLogger.add_appender(io: $stdout) +end + +# Print out the request and response when DEBUG=true +RSpec.configure do | config | + config.after(:each) do | example | + if ENV["DEBUG"] == "true" && defined?(last_response) + last_request.env["rack.input"]&.rewind + puts "------------------------------------------------------------" + puts "Request: #{last_request.request_method} #{last_request.path}\n\n" + puts "Rack env:\n#{last_request.env}\n\n" + puts "Request body:\n#{last_request.env["rack.input"]&.read}" + + puts "\n\n" + puts "Response status: #{last_response.status}\n\n" + puts "Response headers: #{last_response.headers}\n\n" + puts "Response body:\n#{last_response.body}" + puts "------------------------------------------------------------" + puts "" + end + end +end