Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Change Webrat Rails integration to use the Webrat::Methods module
  • Loading branch information
brynary committed Nov 23, 2008
1 parent a8e02a6 commit adf68c2
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 58 deletions.
4 changes: 4 additions & 0 deletions lib/webrat/core/configuration.rb
Expand Up @@ -12,10 +12,14 @@ def self.configuration
end

class Configuration

attr_accessor :mode

# Sets whether to save and open pages with error status codes in a browser
attr_accessor :open_error_files

def initialize
self.mode = :rails
self.open_error_files = true
end

Expand Down
9 changes: 7 additions & 2 deletions lib/webrat/core/methods.rb
Expand Up @@ -5,7 +5,7 @@ def self.delegate_to_session(*meths)
meths.each do |meth|
self.class_eval <<-RUBY
def #{meth}(*args, &blk)
@_webrat_session ||= ::Webrat::MerbSession.new
@_webrat_session ||= ::Webrat.session_class.new(self)
@_webrat_session.#{meth}(*args, &blk)
end
RUBY
Expand Down Expand Up @@ -38,7 +38,12 @@ def #{meth}(*args, &blk)
:clicks_button, :click_button,
:reload, :reloads,
:clicks_link_within, :click_link_within,
:field_labeled
:field_labeled,
:set_hidden_field, :submit_form,
:request_page, :current_dom,
:selects_date, :selects_time, :selects_datetime,
:select_date, :select_time, :select_datetime


end
end
22 changes: 21 additions & 1 deletion lib/webrat/core/session.rb
Expand Up @@ -7,17 +7,37 @@ module Webrat
class PageLoadError < WebratError
end

def self.session_class
case Webrat.configuration.mode
when :rails
RailsSession
when :merb
MerbSession
when :selenium
SeleniumSession
when :rack
RackSession
when :sinatra
SinatraSession
when :mechanize
MechanizeSession
else
raise WebratError.new("Unknown Webrat mode: #{Webrat.configuration.mode.inspect}")
end
end

class Session
extend Forwardable
include Logging

attr_reader :current_url

def initialize #:nodoc:
def initialize(context = nil) #:nodoc:
@http_method = :get
@data = {}
@default_headers = {}
@custom_headers = {}
@context = context
end

# Saves the page out to RAILS_ROOT/tmp/ and opens it in the default
Expand Down
21 changes: 9 additions & 12 deletions lib/webrat/mechanize.rb
Expand Up @@ -6,13 +6,8 @@ class MechanizeSession < Session
attr_accessor :response
alias :page :response

def initialize(mechanize = WWW::Mechanize.new)
super()
@mechanize = mechanize
end

def get(url, data, headers_argument_not_used = nil)
@response = @mechanize.get(url, data)
@response = mechanize.get(url, data)
end

def post(url, data, headers_argument_not_used = nil)
Expand All @@ -25,11 +20,7 @@ def post(url, data, headers_argument_not_used = nil)
end
memo
end
@response = @mechanize.post(url, post_data)
end

def response
@response
@response = mechanize.post(url, post_data)
end

def response_body
Expand All @@ -39,8 +30,14 @@ def response_body
def response_code
@response.code.to_i
end

def mechanize
@mechanize = WWW::Mechanize.new
end

def_delegators :@mechanize, :basic_auth
def_delegators :mechanize, :basic_auth

end
end

Webrat.configuration.mode = :mechanize
2 changes: 2 additions & 0 deletions lib/webrat/merb.rb
Expand Up @@ -73,3 +73,5 @@ def browser
end
end

Webrat.configuration.mode = :merb

4 changes: 3 additions & 1 deletion lib/webrat/rack.rb
Expand Up @@ -21,4 +21,6 @@ def response_code
@response.status
end
end
end
end

Webrat.configuration.mode = :rack
41 changes: 12 additions & 29 deletions lib/webrat/rails.rb
@@ -1,10 +1,5 @@
module Webrat
class RailsSession < Session #:nodoc:

def initialize(integration_session)
super()
@integration_session = integration_session
end

def doc_root
File.expand_path(File.join(RAILS_ROOT, 'public'))
Expand Down Expand Up @@ -40,9 +35,13 @@ def response_code

protected

def integration_session
@context
end

def do_request(http_method, url, data, headers) #:nodoc:
update_protocol(url)
@integration_session.request_via_redirect(http_method, remove_protocol(url), data, headers)
integration_session.request_via_redirect(http_method, remove_protocol(url), data, headers)
end

def remove_protocol(href) #:nodoc:
Expand All @@ -55,14 +54,14 @@ def remove_protocol(href) #:nodoc:

def update_protocol(href) #:nodoc:
if href =~ /^https:/
@integration_session.https!(true)
integration_session.https!(true)
elsif href =~ /^http:/
@integration_session.https!(false)
integration_session.https!(false)
end
end

def response #:nodoc:
@integration_session.response
integration_session.response
end

end
Expand All @@ -71,30 +70,14 @@ def response #:nodoc:
module ActionController
module Integration
class Session #:nodoc:

unless instance_methods.include?("put_via_redirect")
require "webrat/rails/redirect_actions"
include Webrat::RedirectActions
end

def respond_to?(name)
super || webrat_session.respond_to?(name)
end

def method_missing(name, *args, &block)
if webrat_session.respond_to?(name)
webrat_session.send(name, *args, &block)
else
super
end
end

protected

def webrat_session
@webrat_session ||= Webrat::RailsSession.new(self)
end

include Webrat::Methods
end
end
end
end

Webrat.configuration.mode = :rails
1 change: 1 addition & 0 deletions lib/webrat/selenium.rb
Expand Up @@ -2,3 +2,4 @@
require "selenium/client"
require "webrat/selenium/selenium_session"

Webrat.configuration.mode = :selenium
4 changes: 3 additions & 1 deletion lib/webrat/sinatra.rb
Expand Up @@ -16,4 +16,6 @@ class SinatraSession < RackSession #:nodoc:
end

end
end
end

Webrat.configuration.mode = :sinatra
8 changes: 5 additions & 3 deletions spec/webrat/core/configuration_spec.rb
Expand Up @@ -9,11 +9,12 @@
Webrat.reset_for_test
end

it "should have a default config" do
Webrat.configuration.should be_an_instance_of(Webrat::Configuration)
it "should default to Rails mode" do
config = Webrat.configuration
config.mode.should == :rails
end

it "should set default values" do
it "should open error files by default" do
config = Webrat.configuration
config.open_error_files.should == true
end
Expand All @@ -22,6 +23,7 @@
Webrat.configure do |config|
config.open_error_files = false
end

config = Webrat.configuration
config.open_error_files.should == false
end
Expand Down
6 changes: 3 additions & 3 deletions spec/webrat/mechanize/mechanize_session_spec.rb
Expand Up @@ -27,10 +27,10 @@ def flattened_data
end

it "should flatten model post data" do
mechanize = mock :mechanize
mechanize = mock(:mechanize)
WWW::Mechanize.stub!(:new => mechanize)
mechanize.should_receive(:post).with(url, flattened_data)

Webrat::MechanizeSession.new(mechanize).post(url, data)
Webrat::MechanizeSession.new.post(url, data)
end
end
end
6 changes: 0 additions & 6 deletions spec/webrat/rails/rails_session_spec.rb
@@ -1,12 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/helper')

describe Webrat::RailsSession do
it "should require a Rails Integration session to be initialized" do
lambda {
Webrat::RailsSession.new
}.should raise_error
end

it "should delegate response_body to the session response body" do
response = mock("response", :body => "<html>")
integration_session = mock("integration session", :response => response)
Expand Down

0 comments on commit adf68c2

Please sign in to comment.