Restructuring files

This commit is contained in:
Bryan Helmkamp 2008-11-05 18:01:05 -05:00
parent 828b7d4c2c
commit ce9235f0fa
8 changed files with 135 additions and 133 deletions

View File

@ -10,9 +10,12 @@ module Webrat
end end
end end
# require "webrat/merb/param_parser"
# require "webrat/merb/url_encoded_pair_parser"
require "webrat/core" require "webrat/core"
require "webrat/rails" if defined?(RAILS_ENV) # TODO: This is probably not a good idea.
require "webrat/merb" if defined?(Merb) # Probably better for webrat users to require "webrat/rails" etc. directly
if defined?(RAILS_ENV)
require "webrat/rails"
elsif defined?(Merb)
require "webrat/merb"
end

View File

@ -1,3 +1,28 @@
require "mechanize" require "mechanize"
require "webrat/mechanize/mechanize_session"
module Webrat
class MechanizeSession < Session
def initialize(mechanize = WWW::Mechanize.new)
super()
@mechanize = mechanize
end
def get(url, data)
@mechanize_page = @mechanize.get(url, data)
end
def post(url, data)
@mechanize_page = @mechanize.post(url, data)
end
def response_body
@mechanize_page.content
end
def response_code
@mechanize_page.code.to_i
end
end
end

View File

@ -1,26 +0,0 @@
module Webrat
class MechanizeSession < Session
def initialize(mechanize = WWW::Mechanize.new)
super()
@mechanize = mechanize
end
def get(url, data)
@mechanize_page = @mechanize.get(url, data)
end
def post(url, data)
@mechanize_page = @mechanize.post(url, data)
end
def response_body
@mechanize_page.content
end
def response_code
@mechanize_page.code.to_i
end
end
end

View File

@ -1,4 +1,101 @@
require "webrat/core" require "webrat"
require "webrat/rails/redirect_actions"
require "webrat/rails/rails_session" module Webrat
require "webrat/rails/session" class RailsSession < Session
def initialize(integration_session)
super()
@integration_session = integration_session
end
def doc_root
File.expand_path(File.join(RAILS_ROOT, 'public'))
end
def saved_page_dir
File.expand_path(File.join(RAILS_ROOT, "tmp"))
end
def get(url, data, headers = nil)
update_protocol(url)
@integration_session.get_via_redirect(remove_protocol(url), data, headers)
end
def post(url, data, headers = nil)
update_protocol(url)
@integration_session.post_via_redirect(remove_protocol(url), data, headers)
end
def put(url, data, headers = nil)
update_protocol(url)
@integration_session.put_via_redirect(remove_protocol(url), data, headers)
end
def delete(url, data, headers = nil)
update_protocol(url)
@integration_session.delete_via_redirect(remove_protocol(url), data, headers)
end
def response_body
response.body
end
def response_code
response.code.to_i
end
protected
def remove_protocol(href)
if href =~ %r{^https?://www.example.com(/.*)}
$LAST_MATCH_INFO.captures.first
else
href
end
end
def update_protocol(href)
if href =~ /^https:/
@integration_session.https!(true)
elsif href =~ /^http:/
@integration_session.https!(false)
end
end
def response
@integration_session.response
end
end
end
module ActionController
module Integration
class Session
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
end
end
end

View File

@ -1,68 +0,0 @@
module Webrat
class RailsSession < Session
def initialize(integration_session)
super()
@integration_session = integration_session
end
def doc_root
File.expand_path(File.join(RAILS_ROOT, 'public'))
end
def saved_page_dir
File.expand_path(File.join(RAILS_ROOT, "tmp"))
end
def get(url, data, headers = nil)
update_protocol(url)
@integration_session.get_via_redirect(remove_protocol(url), data, headers)
end
def post(url, data, headers = nil)
update_protocol(url)
@integration_session.post_via_redirect(remove_protocol(url), data, headers)
end
def put(url, data, headers = nil)
update_protocol(url)
@integration_session.put_via_redirect(remove_protocol(url), data, headers)
end
def delete(url, data, headers = nil)
update_protocol(url)
@integration_session.delete_via_redirect(remove_protocol(url), data, headers)
end
def response_body
response.body
end
def response_code
response.code.to_i
end
protected
def remove_protocol(href)
if href =~ %r{^https?://www.example.com(/.*)}
$LAST_MATCH_INFO.captures.first
else
href
end
end
def update_protocol(href)
if href =~ /^https:/
@integration_session.https!(true)
elsif href =~ /^http:/
@integration_session.https!(false)
end
end
def response
@integration_session.response
end
end
end

View File

@ -1,29 +0,0 @@
module ActionController
module Integration
class Session
unless instance_methods.include?("put_via_redirect")
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
end
end
end

View File

@ -1,4 +1,4 @@
require 'webrat/rack/rack_session' require 'webrat/rack'
require 'sinatra' require 'sinatra'
require 'sinatra/test/methods' require 'sinatra/test/methods'