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
|
namespace :spec do
|
||||||
desc "Run the integration specs"
|
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
|
namespace :integration do
|
||||||
desc "Run the Rails integration specs"
|
desc "Run the Rails integration specs"
|
||||||
@ -140,7 +140,15 @@ namespace :spec do
|
|||||||
task :sinatra do
|
task :sinatra do
|
||||||
Dir.chdir "spec/integration/sinatra" do
|
Dir.chdir "spec/integration/sinatra" do
|
||||||
result = system "rake test"
|
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
|
end
|
||||||
end
|
end
|
||||||
|
@ -16,7 +16,11 @@ module Webrat
|
|||||||
end
|
end
|
||||||
|
|
||||||
def webrat_session
|
def webrat_session
|
||||||
@_webrat_session ||= ::Webrat.session_class.new(self)
|
if Webrat.configuration.mode == :rack_test
|
||||||
|
@_webrat_session ||= ::Webrat::RackTestSession.new(rack_test_session)
|
||||||
|
else
|
||||||
|
@_webrat_session ||= ::Webrat.session_class.new(self)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# all of these methods delegate to the @session, which should
|
# all of these methods delegate to the @session, which should
|
||||||
|
@ -26,6 +26,8 @@ module Webrat
|
|||||||
SinatraSession
|
SinatraSession
|
||||||
when :mechanize
|
when :mechanize
|
||||||
MechanizeSession
|
MechanizeSession
|
||||||
|
when :rack_test
|
||||||
|
RackTestSession
|
||||||
else
|
else
|
||||||
raise WebratError.new(<<-STR)
|
raise WebratError.new(<<-STR)
|
||||||
Unknown Webrat mode: #{Webrat.configuration.mode.inspect}
|
Unknown Webrat mode: #{Webrat.configuration.mode.inspect}
|
||||||
@ -122,11 +124,8 @@ For example:
|
|||||||
|
|
||||||
url = canonicalize_url(url)
|
url = canonicalize_url(url)
|
||||||
debug_log "REQUESTING PAGE: #{http_method.to_s.upcase} #{url} with #{data.inspect} and HTTP headers #{h.inspect}"
|
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 || {}
|
process_request(http_method, url, data, h)
|
||||||
else
|
|
||||||
send "#{http_method}", url, data || {}, h
|
|
||||||
end
|
|
||||||
|
|
||||||
save_and_open_page if exception_caught? && Webrat.configuration.open_error_files?
|
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?
|
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
|
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
|
def response_location
|
||||||
canonicalize_url(response.headers["Location"])
|
canonicalize_url(response.headers["Location"])
|
||||||
end
|
end
|
||||||
|
32
lib/webrat/rack_test.rb
Normal file
32
lib/webrat/rack_test.rb
Normal file
@ -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
|
5
spec/integration/rack/Rakefile
Normal file
5
spec/integration/rack/Rakefile
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
require "rake/testtask"
|
||||||
|
|
||||||
|
Rake::TestTask.new do |t|
|
||||||
|
t.test_files = FileList["test/*_test.rb"]
|
||||||
|
end
|
16
spec/integration/rack/rack_app.rb
Normal file
16
spec/integration/rack/rack_app.rb
Normal file
@ -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
|
20
spec/integration/rack/test/test_helper.rb
Normal file
20
spec/integration/rack/test/test_helper.rb
Normal file
@ -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
|
67
spec/integration/rack/test/webrat_rack_test.rb
Normal file
67
spec/integration/rack/test/webrat_rack_test.rb
Normal file
@ -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
Block a user