Last active
September 9, 2015 14:16
-
-
Save iGEL/e5a59e396b278e853e37 to your computer and use it in GitHub Desktop.
Patch WebMock to print the curl command on disabled net connect. Just copy & paste it to execute.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module WebMock | |
class NetConnectNotAllowedError < Exception | |
alias_method :stubbing_instructions_without_curl, :stubbing_instructions | |
private | |
def stubbing_instructions(request_signature) | |
[ | |
stubbing_instructions_without_curl(request_signature), | |
CurlInstructions.new(request_signature).instructions | |
].compact.join("\n\n") | |
end | |
class CurlInstructions | |
def initialize(request_signature) | |
@request_signature = request_signature | |
end | |
def instructions | |
"You can get this request with this command: \n\n" + ["curl", method, uri, headers, body].compact.join(" ") | |
end | |
private | |
def method | |
return if request_signature.method == :get | |
"-X#{request_signature.method}".upcase | |
end | |
def uri | |
request_signature.uri | |
end | |
def headers | |
return if request_signature.headers.empty? | |
request_signature.headers.map { |k,v| %(-H #{(k + ": " + v).inspect})} | |
end | |
def body | |
return if request_signature.body.nil? | |
"-d #{request_signature.body.inspect}" | |
end | |
attr_reader :request_signature | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment