webrat/lib/webrat/sinatra.rb
Simon Rozet 90baf3311c Sinatra now provides sensible options for testing
In the lastest release, 0.9.1:

* Set sensible defaults on Default for test env [1]
  5a33a9586f

* Set :environment to :test in TestHarness
  9af37724e0

* Set :environment to :test when Sinatra::Test is included
  93b19a3492

[1] Only Sinatra::Default provides special options for testing. If you subclass
    Sinatra::Base, you'll have to set them yourself.

foo
2009-03-08 23:41:34 +01:00

45 lines
923 B
Ruby

require "webrat/rack"
require "sinatra/test"
module Webrat
class SinatraSession < RackSession
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
%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