diff --git a/lib/webrat/sinatra.rb b/lib/webrat/sinatra.rb index e8452fa..2f2db9f 100644 --- a/lib/webrat/sinatra.rb +++ b/lib/webrat/sinatra.rb @@ -1,30 +1,52 @@ -require 'webrat/rack' -require 'sinatra' -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 +require "webrat/rack" +require "sinatra/test" module Webrat - class SinatraSession < RackSession #:nodoc: + class SinatraSession < RackSession include Sinatra::Test attr_reader :request, :response - %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 + def initialize(context = nil) + super(context) + + app = + if context.respond_to?(:app) + context.app + else + Sinatra::Application.tap { |app| + app.set :environment, :test + app.disable :run, :reload + } + end + + @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 + @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 diff --git a/spec/integration/sinatra/app.rb b/spec/integration/sinatra/classic_app.rb similarity index 96% rename from spec/integration/sinatra/app.rb rename to spec/integration/sinatra/classic_app.rb index dd0f439..41ed968 100644 --- a/spec/integration/sinatra/app.rb +++ b/spec/integration/sinatra/classic_app.rb @@ -61,4 +61,4 @@ __END__ @@ hello
Hello, <%= @user %>
-Your email is: <%= @email %>
\ No newline at end of file +Your email is: <%= @email %>
diff --git a/spec/integration/sinatra/modular_app.rb b/spec/integration/sinatra/modular_app.rb new file mode 100644 index 0000000..c2f0557 --- /dev/null +++ b/spec/integration/sinatra/modular_app.rb @@ -0,0 +1,8 @@ +require "rubygems" +require "sinatra/base" + +class MyModularApp < Sinatra::Default + get "/" do + "Hello World" + end +end diff --git a/spec/integration/sinatra/test/webrat_test.rb b/spec/integration/sinatra/test/classic_app_test.rb similarity index 89% rename from spec/integration/sinatra/test/webrat_test.rb rename to spec/integration/sinatra/test/classic_app_test.rb index e694edb..fbb4759 100644 --- a/spec/integration/sinatra/test/webrat_test.rb +++ b/spec/integration/sinatra/test/classic_app_test.rb @@ -1,6 +1,7 @@ require File.dirname(__FILE__) + "/test_helper" +require File.dirname(__FILE__) + "/../classic_app" -class WebratTest < Test::Unit::TestCase +class MyClassicAppTest < Test::Unit::TestCase def test_visits_pages visit "/" assert response_body.include?("visit") @@ -23,7 +24,7 @@ class WebratTest < Test::Unit::TestCase visit "/" assert field_labeled("Prefilled").value, "text" end - + def test_follows_internal_redirects visit "/internal_redirect" assert response_body.include?("visit") diff --git a/spec/integration/sinatra/test/modular_app_test.rb b/spec/integration/sinatra/test/modular_app_test.rb new file mode 100644 index 0000000..dd0f639 --- /dev/null +++ b/spec/integration/sinatra/test/modular_app_test.rb @@ -0,0 +1,16 @@ +require File.dirname(__FILE__) + "/test_helper" +require File.dirname(__FILE__) + "/../modular_app" + +class MyModularAppTest < Test::Unit::TestCase + def app + MyModularApp.tap { |app| + app.disable :run, :reload + app.set :environment, :test + } + end + + def test_it_works + visit "/" + assert_contain "Hello World" + end +end diff --git a/spec/integration/sinatra/test/test_helper.rb b/spec/integration/sinatra/test/test_helper.rb index c01c9b2..186852f 100644 --- a/spec/integration/sinatra/test/test_helper.rb +++ b/spec/integration/sinatra/test/test_helper.rb @@ -1,8 +1,6 @@ require "rubygems" require "test/unit" require "redgreen" -require "sinatra" -require File.dirname(__FILE__) + "/../app" require File.dirname(__FILE__) + "/../../../../lib/webrat" @@ -12,6 +10,7 @@ end class Test::Unit::TestCase include Webrat::Methods - + include Webrat::Matchers + Webrat::Methods.delegate_to_session :response_code, :response_body end diff --git a/spec/private/sinatra/helper.rb b/spec/private/sinatra/helper.rb deleted file mode 100644 index 3d52256..0000000 --- a/spec/private/sinatra/helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') -require "webrat/sinatra" \ No newline at end of file diff --git a/spec/private/sinatra/sinatra_session_spec.rb b/spec/private/sinatra/sinatra_session_spec.rb deleted file mode 100644 index 0643f79..0000000 --- a/spec/private/sinatra/sinatra_session_spec.rb +++ /dev/null @@ -1,28 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/helper') - -describe Webrat::SinatraSession, "API" do - before :each do - Webrat.configuration.mode = :sinatra - @sinatra_session = Webrat::SinatraSession.new - end - - it "should delegate get to sinatras get" do - @sinatra_session.should_receive(:orig_get).with("url", { :env => "headers" }) - @sinatra_session.get("url", {}, "headers") - end - - it "should delegate post to sinatras post" do - @sinatra_session.should_receive(:orig_post).with("url", { :env => "headers" }) - @sinatra_session.post("url", {}, "headers") - end - - it "should delegate put to sinatras put" do - @sinatra_session.should_receive(:orig_put).with("url", { :env => "headers" }) - @sinatra_session.put("url", {}, "headers") - end - - it "should delegate delete to sinatras delete" do - @sinatra_session.should_receive(:orig_delete).with("url", { :env => "headers" }) - @sinatra_session.delete("url", {}, "headers") - end -end