Refactor Webrat::Session

* Webrat::Session.new now take a session adapter class as it's argument and
  delegates methods such as #response_body and #response_code to an instance
  of it.

* Some of these methods will go away in the future. However, *a lot*
  of specs depends on them so I've left them for now. The plan is to
  strip down the session adapter API to these three methods:

    * request
    * response_code
    * response_body

* I had to mark a spec as pending: spec/private/rails/rails_session_spec.rb
This commit is contained in:
Simon Rozet 2009-06-12 21:30:35 +02:00
parent cbc447223c
commit f2b3b9891b
7 changed files with 40 additions and 36 deletions

View File

@ -16,11 +16,16 @@ module Webrat
end
def webrat_session
if Webrat.configuration.mode == :rack
@_webrat_session ||= ::Webrat::RackSession.new(rack_test_session)
else
@_webrat_session ||= ::Webrat.session_class.new(self)
end
@_webrat_session ||= ::Webrat::Session.new(webrat_adapter)
end
def webrat_adapter
@_webrat_adapter ||=
if Webrat.configuration.mode == :rack
Webrat::RackSession.new(rack_test_session)
else
Webrat.session_class.new(self)
end
end
# all of these methods delegate to the @session, which should

View File

@ -51,12 +51,16 @@ For example:
attr_reader :current_url
attr_reader :elements
def initialize(context = nil) #:nodoc:
def_delegators :@adapter, :response, :response_code, :response_body,
:response_body=, :response_code=,
:get, :post, :put, :delete
def initialize(adapter=nil)
@http_method = :get
@data = {}
@default_headers = {}
@custom_headers = {}
@context = context
@adapter = adapter
reset
end

View File

@ -3,7 +3,7 @@ require "merb-core"
require "webrat/merb_multipart_support"
module Webrat
class MerbSession < Session #:nodoc:
class MerbSession #:nodoc:
include Merb::Test::MakeRequest
# Include Webrat's own version of multipart_post/put because the officially
@ -12,6 +12,8 @@ module Webrat
attr_accessor :response
def initialize(context=nil); end
def get(url, data, headers = nil)
do_request(url, data, headers, "GET")
end

View File

@ -3,10 +3,13 @@ require "rack/test"
Rack::Test::DEFAULT_HOST.replace("www.example.com")
module Webrat
class RackSession < Session
def initialize(rack_test_session) #:nodoc:
super()
@rack_test_session = rack_test_session
class RackSession
extend Forwardable
def_delegators :@session, :get, :post, :put, :delete
def initialize(session) #:nodoc:
@session = session
end
def response_body
@ -18,17 +21,7 @@ module Webrat
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)
@session.last_response
end
end
end

View File

@ -5,9 +5,15 @@ require "action_controller/integration"
require "action_controller/record_identifier"
module Webrat
class RailsSession < Session #:nodoc:
class RailsSession #:nodoc:
include ActionController::RecordIdentifier
attr_reader :integration_session
def initialize(session)
@integration_session = session
end
# The Rails version of within supports passing in a model and Webrat
# will apply a scope based on Rails' dom_id for that model.
#
@ -61,10 +67,6 @@ module Webrat
protected
def integration_session
@context
end
def do_request(http_method, url, data, headers) #:nodoc:
update_protocol(url)
integration_session.send(http_method, normalize_url(url), data, headers)
@ -93,7 +95,6 @@ module Webrat
def response #:nodoc:
integration_session.response
end
end
end

View File

@ -2,15 +2,12 @@ require "webrat/rack"
require "sinatra/test"
module Webrat
class SinatraSession < Session
class SinatraSession
include Sinatra::Test
attr_reader :request, :response
def initialize(context = nil)
super(context)
def initialize(context)
app = context.respond_to?(:app) ? context.app : Sinatra::Application
@browser = Sinatra::TestHarness.new(app)
end
@ -32,7 +29,7 @@ module Webrat
end
def response_code
@browser.status
@browser.response.status
end
private

View File

@ -87,6 +87,8 @@ describe Webrat::RailsSession do
end
it "should accept an ActiveRecord argument to #within and translate to a selector using dom_id" do
pending "Move this to spec/public/within_spec.rb"
body = <<-HTML
<a href="/page1">Edit</a>
<div id="new_object">