Skip to content

Instantly share code, notes, and snippets.

@brentd
Created August 2, 2019 16:39
Show Gist options
  • Save brentd/6bdb940f8b16b761aae868ce8d9dc060 to your computer and use it in GitHub Desktop.
Save brentd/6bdb940f8b16b761aae868ce8d9dc060 to your computer and use it in GitHub Desktop.
Webmock stubbing helper
# Quick way to log both stubbing instructions and the response of a real request.
# Useful for stubbing real requests without having to resort to VCR.
WebMock.allow_net_connect!
WebMock.after_request do |request_signature, response|
puts "\nA real HTTP request was made."
puts WebMock::RequestSignatureSnippet.new(request_signature).stubbing_instructions
puts "----- RESPONSE HEADERS -----"
pp response.headers
puts "----- RESPONSE BODY -----"
if response.headers["Content-Type"] =~ /json/
puts JSON.pretty_generate(JSON.parse(response.body))
else
puts response.body
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment