Merge commit 'upstream/master'

This commit is contained in:
Mark Menard 2009-03-12 15:41:37 -04:00
commit 73f4c441c1
9 changed files with 80 additions and 57 deletions

View File

@ -2,6 +2,7 @@
* Minor enhancements * Minor enhancements
* Support for "modular" Sinatra app style (Simon Rozet)
* When faced with a label with no for attribute, that contains a hidden field * When faced with a label with no for attribute, that contains a hidden field
and another field, as can be the case in Rails 2.3's checkbox view, and another field, as can be the case in Rails 2.3's checkbox view,
webrat now locates the non-hidden field. (Luke Melia) webrat now locates the non-hidden field. (Luke Melia)
@ -10,6 +11,10 @@
* Add application_framework config for Selenium mode to determine how to * Add application_framework config for Selenium mode to determine how to
start and stop the app server (Corey Donohoe) start and stop the app server (Corey Donohoe)
* Bug fixes
* Fix following of absolute redirect URL in Sinatra (Simon Rozet)
== 0.4.2 / 2009-02-24 == 0.4.2 / 2009-02-24
* Major enhancements * Major enhancements

View File

@ -1,30 +1,44 @@
require 'webrat/rack' require "webrat/rack"
require 'sinatra' require "sinatra/test"
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 module Webrat
class SinatraSession < RackSession #:nodoc: class SinatraSession < RackSession
include Sinatra::Test include Sinatra::Test
attr_reader :request, :response attr_reader :request, :response
%w(get head post put delete).each do |verb| def initialize(context = nil)
alias_method "orig_#{verb}", verb super(context)
define_method(verb) do |*args| # (path, data, headers = nil)
path, data, headers = *args app = context.respond_to?(:app) ? context.app : Sinatra::Application
data = data.inject({}) {|data, (key,value)| data[key] = Rack::Utils.unescape(value); data } @browser = Sinatra::TestHarness.new(app)
params = data.merge(:env => headers || {})
self.__send__("orig_#{verb}", path, params)
end 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 end
end end

View File

@ -0,0 +1,16 @@
require "rubygems"
require "sinatra/base"
class MyModularApp < Sinatra::Default
get "/" do
"Hello World"
end
get "/redirect_absolute_url" do
redirect URI.join(request.url, "foo").to_s
end
get "/foo" do
"spam"
end
end

View File

@ -1,6 +1,7 @@
require File.dirname(__FILE__) + "/test_helper" 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 def test_visits_pages
visit "/" visit "/"
assert response_body.include?("visit") assert response_body.include?("visit")

View File

@ -0,0 +1,18 @@
require File.dirname(__FILE__) + "/test_helper"
require File.dirname(__FILE__) + "/../modular_app"
class MyModularAppTest < Test::Unit::TestCase
def app
MyModularApp
end
def test_it_works
visit "/"
assert_contain "Hello World"
end
def test_redirects
visit "/redirect_absolute_url"
assert_equal "spam", response_body
end
end

View File

@ -1,8 +1,6 @@
require "rubygems" require "rubygems"
require "test/unit" require "test/unit"
require "redgreen" require "redgreen"
require "sinatra"
require File.dirname(__FILE__) + "/../app"
require File.dirname(__FILE__) + "/../../../../lib/webrat" require File.dirname(__FILE__) + "/../../../../lib/webrat"
@ -12,6 +10,7 @@ end
class Test::Unit::TestCase class Test::Unit::TestCase
include Webrat::Methods include Webrat::Methods
include Webrat::Matchers
Webrat::Methods.delegate_to_session :response_code, :response_body Webrat::Methods.delegate_to_session :response_code, :response_body
end end

View File

@ -1,2 +0,0 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
require "webrat/sinatra"

View File

@ -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