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 end
def webrat_session def webrat_session
if Webrat.configuration.mode == :rack @_webrat_session ||= ::Webrat::Session.new(webrat_adapter)
@_webrat_session ||= ::Webrat::RackSession.new(rack_test_session) end
else
@_webrat_session ||= ::Webrat.session_class.new(self) def webrat_adapter
end @_webrat_adapter ||=
if Webrat.configuration.mode == :rack
Webrat::RackSession.new(rack_test_session)
else
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

View File

@ -51,12 +51,16 @@ For example:
attr_reader :current_url attr_reader :current_url
attr_reader :elements 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 @http_method = :get
@data = {} @data = {}
@default_headers = {} @default_headers = {}
@custom_headers = {} @custom_headers = {}
@context = context @adapter = adapter
reset reset
end end

View File

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

View File

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

View File

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

View File

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

View File

@ -87,6 +87,8 @@ describe Webrat::RailsSession do
end end
it "should accept an ActiveRecord argument to #within and translate to a selector using dom_id" do 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 body = <<-HTML
<a href="/page1">Edit</a> <a href="/page1">Edit</a>
<div id="new_object"> <div id="new_object">