Skip to content

Instantly share code, notes, and snippets.

@erwanlr
Created March 31, 2014 13:42
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 erwanlr/9892510 to your computer and use it in GitHub Desktop.
Save erwanlr/9892510 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'typhoeus'
require 'webmock'
include WebMock::API
module WebMock
class StubRegistry
def evaluate_response_for_request(response, request_signature)
if [301, 302].include?(response.status[0])
request_signature.uri = response.headers['Location']
::WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
response_for_request(request_signature)
else
response.dup.evaluate(request_signature)
end
end
end
end
url = 'https://updown.io:443/'
redirection = 'https://beta.updown.io:443/'
def call_url(url)
response = Typhoeus.get(url, followlocation: true)
puts "#{url} : #{response.code}"
puts "headers_hash : #{response.headers_hash}"
end
puts "Typhoues #{Typhoeus::VERSION}"
puts "webmock #{WebMock::VERSION}"
puts
puts 'Real:'
WebMock.allow_net_connect!
call_url(url)
puts
puts 'Webmock stub:'
WebMock.disable_net_connect!
stub_request(:get, url)
.to_return(status: 301, headers: { location: redirection })
stub_request(:get, redirection)
call_url(url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment