Skip to content

Instantly share code, notes, and snippets.

@thiagomoretto
Created October 11, 2011 21:03
Show Gist options
  • Save thiagomoretto/1279432 to your computer and use it in GitHub Desktop.
Save thiagomoretto/1279432 to your computer and use it in GitHub Desktop.
Rack-Test MockSession that enables to call remote/external URL using HTTParty
# Usage
# @browser ||= Rack::Test::Session.new(Rack::RemoteSession.new(Sinatra::Application, "twitter.com"))
require 'httparty'
module Rack
class RemoteSession < MockSession # :nodoc:
def request(uri, env)
env["HTTP_COOKIE"] ||= cookie_jar.for(uri)
@last_request = Rack::Request.new(env)
response = HTTParty.get("http:#{uri.to_s}")
status, headers, body = response.code, response.headers, response.body
@last_response = MockResponse.new(status, headers, body, env["rack.errors"].flush)
body.close if body.respond_to?(:close)
# bug: cookie_jar.merge(last_response.headers["Set-Cookie"], uri)
@after_request.each { |hook| hook.call }
if @last_response.respond_to?(:finish)
@last_response.finish
else
@last_response
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment