lib/webrat/sinatra.rb in webrat-0.4.2 vs lib/webrat/sinatra.rb in webrat-0.4.3
- old
+ new
@@ -1,30 +1,44 @@
-require 'webrat/rack'
-require 'sinatra'
-require 'sinatra/test'
+require "webrat/rack"
+require "sinatra/test"
-class Sinatra::Application
- # Override this to prevent Sinatra from barfing on the options passed from RSpec
- def self.load_default_options_from_command_line!
- end
-end
-
-disable :run
-disable :reload
-
module Webrat
- class SinatraSession < RackSession #:nodoc:
+ 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|
- alias_method "orig_#{verb}", verb
- define_method(verb) do |*args| # (path, data, headers = nil)
- path, data, headers = *args
- data = data.inject({}) {|data, (key,value)| data[key] = Rack::Utils.unescape(value); data }
- params = data.merge(:env => headers || {})
- self.__send__("orig_#{verb}", path, params)
- end
+ 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