Skip to content

Commit

Permalink
Start on Rack::Test integration
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed May 11, 2009
1 parent 755cf6e commit b525410
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 8 deletions.
12 changes: 10 additions & 2 deletions Rakefile
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/webrat/core/methods.rb
Expand Up @@ -16,7 +16,11 @@ def webrat
end

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

# all of these methods delegate to the @session, which should
Expand Down
17 changes: 12 additions & 5 deletions lib/webrat/core/session.rb
Expand Up @@ -26,6 +26,8 @@ def self.session_class
SinatraSession
when :mechanize
MechanizeSession
when :rack_test
RackTestSession
else
raise WebratError.new(<<-STR)
Unknown Webrat mode: #{Webrat.configuration.mode.inspect}
Expand Down Expand Up @@ -122,11 +124,8 @@ def request_page(url, http_method, data) #:nodoc:

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?
Expand Down Expand Up @@ -281,6 +280,14 @@ def automate

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
Expand Down
32 changes: 32 additions & 0 deletions lib/webrat/rack_test.rb
@@ -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 changes: 5 additions & 0 deletions spec/integration/rack/Rakefile
@@ -0,0 +1,5 @@
require "rake/testtask"

Rake::TestTask.new do |t|
t.test_files = FileList["test/*_test.rb"]
end
16 changes: 16 additions & 0 deletions spec/integration/rack/rack_app.rb
@@ -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 changes: 20 additions & 0 deletions spec/integration/rack/test/test_helper.rb
@@ -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 changes: 67 additions & 0 deletions spec/integration/rack/test/webrat_rack_test.rb
@@ -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

0 comments on commit b525410

Please sign in to comment.