Skip to content

Instantly share code, notes, and snippets.

@iGEL
Last active September 9, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iGEL/e5a59e396b278e853e37 to your computer and use it in GitHub Desktop.
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.
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