Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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