Start on Rack::Test integration
This commit is contained in:
parent
755cf6e508
commit
b5254109f1
12
Rakefile
12
Rakefile
|
@ -106,7 +106,7 @@ end
|
|||
|
||||
namespace :spec do
|
||||
desc "Run the integration specs"
|
||||
task :integration => ["integration:rails", "integration:merb", "integration:sinatra"]
|
||||
task :integration => ["integration:rails", "integration:merb", "integration:sinatra", "integration:rack"]
|
||||
|
||||
namespace :integration do
|
||||
desc "Run the Rails integration specs"
|
||||
|
@ -140,7 +140,15 @@ namespace :spec do
|
|||
task :sinatra do
|
||||
Dir.chdir "spec/integration/sinatra" do
|
||||
result = system "rake test"
|
||||
raise "Sinatra tntegration tests failed" unless result
|
||||
raise "Sinatra integration tests failed" unless result
|
||||
end
|
||||
end
|
||||
|
||||
desc "Run the Sinatra integration specs"
|
||||
task :rack do
|
||||
Dir.chdir "spec/integration/rack" do
|
||||
result = system "rake test"
|
||||
raise "Rack integration tests failed" unless result
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -16,8 +16,12 @@ module Webrat
|
|||
end
|
||||
|
||||
def webrat_session
|
||||
if Webrat.configuration.mode == :rack_test
|
||||
@_webrat_session ||= ::Webrat::RackTestSession.new(rack_test_session)
|
||||
else
|
||||
@_webrat_session ||= ::Webrat.session_class.new(self)
|
||||
end
|
||||
end
|
||||
|
||||
# all of these methods delegate to the @session, which should
|
||||
# be created transparently.
|
||||
|
|
|
@ -26,6 +26,8 @@ module Webrat
|
|||
SinatraSession
|
||||
when :mechanize
|
||||
MechanizeSession
|
||||
when :rack_test
|
||||
RackTestSession
|
||||
else
|
||||
raise WebratError.new(<<-STR)
|
||||
Unknown Webrat mode: #{Webrat.configuration.mode.inspect}
|
||||
|
@ -122,11 +124,8 @@ For example:
|
|||
|
||||
url = canonicalize_url(url)
|
||||
debug_log "REQUESTING PAGE: #{http_method.to_s.upcase} #{url} with #{data.inspect} and HTTP headers #{h.inspect}"
|
||||
if h.empty?
|
||||
send "#{http_method}", url, data || {}
|
||||
else
|
||||
send "#{http_method}", url, data || {}, h
|
||||
end
|
||||
|
||||
process_request(http_method, url, data, h)
|
||||
|
||||
save_and_open_page if exception_caught? && Webrat.configuration.open_error_files?
|
||||
raise PageLoadError.new("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code?
|
||||
|
@ -281,6 +280,14 @@ For example:
|
|||
|
||||
private
|
||||
|
||||
def process_request(http_method, url, data, headers)
|
||||
if headers.empty?
|
||||
send "#{http_method}", url, data || {}
|
||||
else
|
||||
send "#{http_method}", url, data || {}, headers
|
||||
end
|
||||
end
|
||||
|
||||
def response_location
|
||||
canonicalize_url(response.headers["Location"])
|
||||
end
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
module Webrat
|
||||
class RackTestSession < Session
|
||||
|
||||
def initialize(rack_test_session) #:nodoc:
|
||||
super()
|
||||
@rack_test_session = rack_test_session
|
||||
end
|
||||
|
||||
def response_body
|
||||
response.body
|
||||
end
|
||||
|
||||
def response_code
|
||||
response.status
|
||||
end
|
||||
|
||||
def response
|
||||
@rack_test_session.last_response
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def process_request(http_method, url, data = {}, headers = {})
|
||||
headers ||= {}
|
||||
data ||= {}
|
||||
|
||||
env = headers.merge(:params => data, :method => http_method.to_s.upcase)
|
||||
@rack_test_session.request(url, env)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
|
@ -0,0 +1,5 @@
|
|||
require "rake/testtask"
|
||||
|
||||
Rake::TestTask.new do |t|
|
||||
t.test_files = FileList["test/*_test.rb"]
|
||||
end
|
|
@ -0,0 +1,16 @@
|
|||
require "rubygems"
|
||||
require "sinatra/base"
|
||||
|
||||
class RackApp < 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
|
|
@ -0,0 +1,20 @@
|
|||
require "rubygems"
|
||||
require "test/unit"
|
||||
require "rack/test"
|
||||
require "redgreen"
|
||||
|
||||
require File.dirname(__FILE__) + "/../../../../lib/webrat"
|
||||
|
||||
Webrat.configure do |config|
|
||||
config.mode = :rack_test
|
||||
end
|
||||
|
||||
class Test::Unit::TestCase
|
||||
include Rack::Test::Methods
|
||||
include Webrat::Methods
|
||||
include Webrat::Matchers
|
||||
|
||||
def app
|
||||
RackApp.new
|
||||
end
|
||||
end
|
|
@ -0,0 +1,67 @@
|
|||
require File.dirname(__FILE__) + "/test_helper"
|
||||
require File.dirname(__FILE__) + "/../rack_app"
|
||||
|
||||
class WebratRackTest < Test::Unit::TestCase
|
||||
def test_visit_returns_response
|
||||
response = visit "/"
|
||||
assert response.ok?
|
||||
end
|
||||
|
||||
def test_last_response_is_available
|
||||
visit "/"
|
||||
assert last_response.ok?
|
||||
end
|
||||
|
||||
def test_last_request_is_available
|
||||
visit "/"
|
||||
assert_equal "/", last_request.env["PATH_INFO"]
|
||||
end
|
||||
|
||||
def test_redirects
|
||||
visit "/redirect_absolute_url"
|
||||
assert_equal "spam", response_body
|
||||
end
|
||||
|
||||
def test_assertions_after_visit
|
||||
visit "/"
|
||||
assert_contain "Hello World"
|
||||
end
|
||||
|
||||
def test_assertions_after_visit
|
||||
get "/"
|
||||
assert_contain "Hello World"
|
||||
end
|
||||
|
||||
# def test_visits_pages
|
||||
# visit "/"
|
||||
# assert response_body.include?("visit")
|
||||
#
|
||||
# click_link "there"
|
||||
# assert response_body.include?('<form method="post" action="/go">')
|
||||
# end
|
||||
#
|
||||
# def test_submits_form
|
||||
# visit "/go"
|
||||
# fill_in "Name", :with => "World"
|
||||
# fill_in "Email", :with => "world@example.org"
|
||||
# click_button "Submit"
|
||||
#
|
||||
# assert response_body.include?("Hello, World")
|
||||
# assert response_body.include?("Your email is: world@example.org")
|
||||
# end
|
||||
#
|
||||
# def test_check_value_of_field
|
||||
# visit "/"
|
||||
# assert field_labeled("Prefilled").value, "text"
|
||||
# end
|
||||
#
|
||||
# def test_follows_internal_redirects
|
||||
# visit "/internal_redirect"
|
||||
# assert response_body.include?("visit")
|
||||
# end
|
||||
#
|
||||
# def test_does_not_follow_external_redirects
|
||||
# visit "/external_redirect"
|
||||
# assert response_code == 302
|
||||
# end
|
||||
end
|
Loading…
Reference in New Issue