From 682f4d19f92eb906c891b1f0764f8954f1174155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Thu, 16 Oct 2008 15:50:11 +0200 Subject: [PATCH] Decouple from Rails so we can use Webrat with Sinatra --- lib/webrat.rb | 1 - lib/webrat/core/form.rb | 4 +++- lib/webrat/rack/rack_session.rb | 11 +++++++++++ lib/webrat/sinatra/sinatra_session.rb | 8 ++++++-- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/webrat.rb b/lib/webrat.rb index dffddac..8f11582 100644 --- a/lib/webrat.rb +++ b/lib/webrat.rb @@ -3,7 +3,6 @@ module Webrat end require "rubygems" -require "active_support" require File.dirname(__FILE__) + "/webrat/core" require File.dirname(__FILE__) + "/webrat/rails" if defined?(RAILS_ENV) diff --git a/lib/webrat/core/form.rb b/lib/webrat/core/form.rb index 60a5c07..9e55a95 100644 --- a/lib/webrat/core/form.rb +++ b/lib/webrat/core/form.rb @@ -106,11 +106,13 @@ module Webrat def form_action @element["action"].blank? ? @session.current_url : @element["action"] end + + HASH = [Hash, HashWithIndifferentAccess] rescue [Hash] def merge(all_params, new_param) new_param.each do |key, value| case all_params[key] - when Hash, HashWithIndifferentAccess + when *HASH merge_hash_values(all_params[key], value) when Array all_params[key] += value diff --git a/lib/webrat/rack/rack_session.rb b/lib/webrat/rack/rack_session.rb index b283294..e3c2e24 100644 --- a/lib/webrat/rack/rack_session.rb +++ b/lib/webrat/rack/rack_session.rb @@ -1,5 +1,16 @@ require 'webrat' +class CGIMethods + def self.parse_query_parameters(params) + hash = {} + params.split('&').each do |p| + pair = p.split('=') + hash[pair[0]] = pair[1] + end + hash + end +end + module Webrat class RackSession < Session def response_body diff --git a/lib/webrat/sinatra/sinatra_session.rb b/lib/webrat/sinatra/sinatra_session.rb index 597c3b8..f770554 100644 --- a/lib/webrat/sinatra/sinatra_session.rb +++ b/lib/webrat/sinatra/sinatra_session.rb @@ -6,8 +6,12 @@ module Webrat class SinatraSession < RackSession include Sinatra::Test::Methods - def get(*args) - get_it(*args) + def get(url, data, headers = nil) + get_it(url, data) + end + + def post(url, data, headers = nil) + post_it(url, data) end end end \ No newline at end of file