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