webrat/lib/webrat/sinatra.rb

45 lines
923 B
Ruby
Raw Normal View History

require "webrat/rack"
require "sinatra/test"
2008-12-29 05:37:12 +00:00
module Webrat
class SinatraSession < RackSession
2009-01-16 17:51:01 +00:00
include Sinatra::Test
attr_reader :request, :response
def initialize(context = nil)
super(context)
app = context.respond_to?(:app) ? context.app : Sinatra::Application
@browser = Sinatra::TestHarness.new(app)
end
2008-10-17 10:38:59 +00:00
%w(get head post put delete).each do |verb|
class_eval <<-RUBY
def #{verb}(path, data, headers = {})
params = data.inject({}) do |data, (key,value)|
data[key] = Rack::Utils.unescape(value)
data
end
headers["HTTP_HOST"] = "www.example.com"
@browser.#{verb}(path, params, headers)
end
RUBY
end
def response_body
@browser.body
end
def response_code
@browser.status
end
private
def response
@browser.response
end
end
end