Skip to content

Commit

Permalink
chore: print requests and responses to stdout when DEBUG=true
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jan 18, 2024
1 parent a2aacd8 commit b950e50
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
5 changes: 0 additions & 5 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 25 additions & 1 deletion spec/support/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b950e50

Please sign in to comment.