Skip to content

Instantly share code, notes, and snippets.

@wycats
Created October 29, 2008 05:22
Show Gist options
  • Save wycats/20628 to your computer and use it in GitHub Desktop.
Save wycats/20628 to your computer and use it in GitHub Desktop.
require "webrat/core"
module Webrat
class Session
include Merb::Test::MakeRequest
attr_accessor :response
def get(url, data, headers = nil)
do_request(url, data, headers, "GET")
end
def post(url, data, headers = nil)
do_request(url, data, headers, "POST")
end
def put(url, data, headers = nil)
do_request(url, data, headers, "PUT")
end
def delete(url, data, headers = nil)
do_request(url, data, headers, "DELETE")
end
def response_body
@response.body.to_s
end
def response_code
@response.status
end
def do_request(url, data, headers, method)
@response = request(url,
:params => (data && data.any?) ? data : nil,
:headers => headers, :method => method)
self.get(@response.headers['Location'], nil, @response.headers) if @response.status == 302
end
def follow_redirect
self.get(@response.headers['Location'], nil, @response.headers) if @response.status == 302
end
end
end
module Merb
module Test
module RequestHelper
def request(uri, env = {})
@session ||= Webrat::Session.new
@session.response = @session.request(uri, env)
end
def follow_redirect
@session.follow_redirect
end
end
end
end
class Merb::Test::RspecStory
def browser
@browser ||= Webrat::Session.new
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment