public
Created

  • Download Gist
example.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
require 'rubygems'
require 'net/http'
require 'webmock'
 
original_request_uri = nil
WebMock.after_request do |request, response|
original_request_uri = request.uri
end
 
host = "encrypted.google.com"
 
http = Net::HTTP.new(host, 443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
 
url = "/search?abc=%2B"
 
WebMock.allow_net_connect!
http.request(Net::HTTP::Post.new(url))
 
WebMock.disable_net_connect!
WebMock.stub_request(:post, original_request_uri.to_s).to_return(:body => 'stubbed')
 
http.request(Net::HTTP::Post.new(url))
output.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
✗ ruby example.rb
/Users/myron/.rvm/gems/ruby-1.8.7-p334/gems/webmock-1.7.0/lib/webmock/http_lib_adapters/net_http.rb:113:in `request': Real HTTP connections are disabled. Unregistered request: POST https://encrypted.google.com/search?abc=+ with headers {'Accept'=>'*/*'} (WebMock::NetConnectNotAllowedError)
 
You can stub this request with the following snippet:
 
stub_request(:post, "https://encrypted.google.com/search?abc=%20").
with(:headers => {'Accept'=>'*/*'}).
to_return(:status => 200, :body => "", :headers => {})
 
registered request stubs:
 
stub_request(:post, "https://encrypted.google.com/search?abc=%20")
 
============================================================
from example.rb:24

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.