提交 5ce25aec 编写于 作者: C Carl Lerche

Merge branch 'configuration_refactor'

require 'active_support/ordered_options'
module AbstractController
class Error < StandardError; end
class ActionNotFound < StandardError; end
......@@ -28,6 +30,10 @@ def descendants
@descendants ||= []
end
def config
@config ||= ActiveSupport::InheritableOptions.new(superclass < Base ? superclass.config : {})
end
# A list of all internal methods for a controller. This finds the first
# abstract superclass of a controller, and gets a list of all public
# instance methods on that abstract class. Public instance methods of
......@@ -95,6 +101,10 @@ def initialize #:nodoc:
@_formats = nil
end
def config
@config ||= ActiveSupport::InheritableOptions.new(self.class.config)
end
# Calls the action going through the entire action dispatch stack.
#
# The actual method that is called is determined by calling
......
......@@ -7,16 +7,17 @@ module Compatibility
class ::ActionController::ActionControllerError < StandardError #:nodoc:
end
module ClassMethods
end
# Temporary hax
included do
::ActionController::UnknownAction = ::AbstractController::ActionNotFound
::ActionController::DoubleRenderError = ::AbstractController::DoubleRenderError
cattr_accessor :session_options
self.session_options = {}
cattr_accessor :relative_url_root
self.relative_url_root = ENV['RAILS_RELATIVE_URL_ROOT']
# ROUTES TODO: This should be handled by a middleware and route generation
# should be able to handle SCRIPT_NAME
self.config.relative_url_root = ENV['RAILS_RELATIVE_URL_ROOT']
class << self
delegate :default_charset=, :to => "ActionDispatch::Response"
......@@ -31,11 +32,24 @@ class << self
@_response)
# Controls the resource action separator
cattr_accessor :resource_action_separator
self.resource_action_separator = "/"
def self.resource_action_separator
@resource_action_separator ||= "/"
end
cattr_accessor :use_accept_header
self.use_accept_header = true
def self.resource_action_separator=(val)
ActiveSupport::Deprecation.warn "ActionController::Base.resource_action_separator is deprecated and only " \
"works with the deprecated router DSL."
@resource_action_separator = val
end
def self.use_accept_header
ActiveSupport::Deprecation.warn "ActionController::Base.use_accept_header doesn't do anything anymore. " \
"The accept header is always taken into account."
end
def self.use_accept_header=(val)
use_accept_header
end
self.page_cache_directory = defined?(Rails.public_path) ? Rails.public_path : ""
......@@ -43,13 +57,44 @@ class << self
# and images to a dedicated asset server away from the main web server. Example:
# ActionController::Base.asset_host = "http://assets.example.com"
cattr_accessor :asset_host
end
def self.deprecated_config_accessor(option, message = nil)
deprecated_config_reader(option, message)
deprecated_config_writer(option, message)
end
cattr_accessor :ip_spoofing_check
self.ip_spoofing_check = true
def self.deprecated_config_reader(option, message = nil)
message ||= "Reading #{option} directly from ActionController::Base is deprecated. " \
"Please read it from config.#{option}"
cattr_accessor :trusted_proxies
ClassMethods.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{option}
ActiveSupport::Deprecation.warn #{message.inspect}, caller
config.#{option}
end
RUBY
end
def self.deprecated_config_writer(option, message = nil)
message ||= "Setting #{option} directly on ActionController::Base is deprecated. " \
"Please set it on config.action_controller.#{option}"
ClassMethods.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{option}=(val)
ActiveSupport::Deprecation.warn #{message.inspect}, caller
config.#{option} = val
end
RUBY
end
deprecated_config_writer :session_store
deprecated_config_writer :session_options
deprecated_config_accessor :relative_url_root, "relative_url_root is ineffective. Please stop using it"
deprecated_config_accessor :assets_dir
deprecated_config_accessor :javascripts_dir
deprecated_config_accessor :stylesheets_dir
# For old tests
def initialize_template_class(*) end
def assign_shortcuts(*) end
......@@ -67,28 +112,52 @@ def process_action(*)
module ClassMethods
def consider_all_requests_local
ActiveSupport::Deprecation.warn "ActionController::Base.consider_all_requests_local is deprecated, " <<
"use Rails.application.config.consider_all_requests_local instead"
"use Rails.application.config.consider_all_requests_local instead", caller
Rails.application.config.consider_all_requests_local
end
def consider_all_requests_local=(value)
ActiveSupport::Deprecation.warn "ActionController::Base.consider_all_requests_local= is no longer effective. " <<
"Please configure it on your application with config.consider_all_requests_local="
ActiveSupport::Deprecation.warn "ActionController::Base.consider_all_requests_local= is deprecated. " <<
"Please configure it on your application with config.consider_all_requests_local=", caller
Rails.application.config.consider_all_requests_local = value
end
def allow_concurrency
ActiveSupport::Deprecation.warn "ActionController::Base.allow_concurrency is deprecated, " <<
"use Rails.application.config.allow_concurrency instead"
"use Rails.application.config.allow_concurrency instead", caller
Rails.application.config.allow_concurrency
end
def allow_concurrency=(value)
ActiveSupport::Deprecation.warn "ActionController::Base.allow_concurrency= is no longer effective. " <<
"Please configure it on your application with config.allow_concurrency="
ActiveSupport::Deprecation.warn "ActionController::Base.allow_concurrency= is deprecated. " <<
"Please configure it on your application with config.allow_concurrency=", caller
Rails.application.config.allow_concurrency = value
end
def ip_spoofing_check=(value)
ActiveSupport::Deprecation.warn "ActionController::Base.ip_spoofing_check= is deprecated. " <<
"Please configure it on your application with config.action_dispatch.ip_spoofing_check=", caller
Rails.application.config.action_disaptch.ip_spoofing_check = value
end
def ip_spoofing_check
ActiveSupport::Deprecation.warn "ActionController::Base.ip_spoofing_check is deprecated. " <<
"Configuring ip_spoofing_check on the application configures a middleware.", caller
Rails.application.config.action_disaptch.ip_spoofing_check
end
def trusted_proxies=(value)
ActiveSupport::Deprecation.warn "ActionController::Base.trusted_proxies= is deprecated. " <<
"Please configure it on your application with config.action_dispatch.trusted_proxies=", caller
Rails.application.config.action_dispatch.ip_spoofing_check = value
end
def trusted_proxies
ActiveSupport::Deprecation.warn "ActionController::Base.trusted_proxies is deprecated. " <<
"Configuring trusted_proxies on the application configures a middleware.", caller
Rails.application.config.action_dispatch.ip_spoofing_check = value
end
def rescue_action(env)
raise env["action_dispatch.rescue.exception"]
end
......
......@@ -165,7 +165,7 @@ def authenticate_or_request_with_http_digest(realm = "Application", &password_pr
# Authenticate with HTTP Digest, returns true or false
def authenticate_with_http_digest(realm = "Application", &password_procedure)
HttpAuthentication::Digest.authenticate(request, realm, &password_procedure)
HttpAuthentication::Digest.authenticate(config.session_options[:secret], request, realm, &password_procedure)
end
# Render output including the HTTP Digest authentication header
......@@ -175,8 +175,8 @@ def request_http_digest_authentication(realm = "Application", message = nil)
end
# Returns false on a valid response, true otherwise
def authenticate(request, realm, &password_procedure)
authorization(request) && validate_digest_response(request, realm, &password_procedure)
def authenticate(secret_key, request, realm, &password_procedure)
authorization(request) && validate_digest_response(secret_key, request, realm, &password_procedure)
end
def authorization(request)
......@@ -189,16 +189,16 @@ def authorization(request)
# Returns false unless the request credentials response value matches the expected value.
# First try the password as a ha1 digest password. If this fails, then try it as a plain
# text password.
def validate_digest_response(request, realm, &password_procedure)
def validate_digest_response(secret_key, request, realm, &password_procedure)
credentials = decode_credentials_header(request)
valid_nonce = validate_nonce(request, credentials[:nonce])
valid_nonce = validate_nonce(secret_key, request, credentials[:nonce])
if valid_nonce && realm == credentials[:realm] && opaque == credentials[:opaque]
if valid_nonce && realm == credentials[:realm] && opaque(secret_key) == credentials[:opaque]
password = password_procedure.call(credentials[:username])
return false unless password
method = request.env['rack.methodoverride.original_method'] || request.env['REQUEST_METHOD']
uri = credentials[:uri][0,1] == '/' ? request.request_uri : request.url
uri = credentials[:uri][0,1] == '/' ? request.fullpath : request.url
[true, false].any? do |password_is_ha1|
expected = expected_response(method, uri, credentials, password, password_is_ha1)
......@@ -238,6 +238,9 @@ def decode_credentials(header)
end
def authentication_header(controller, realm)
secret_key = controller.config.session_options[:secret]
nonce = self.nonce(secret_key)
opaque = opaque(secret_key)
controller.headers["WWW-Authenticate"] = %(Digest realm="#{realm}", qop="auth", algorithm=MD5, nonce="#{nonce}", opaque="#{opaque}")
end
......@@ -280,7 +283,7 @@ def authentication_request(controller, realm, message = nil)
# The nonce is opaque to the client. Composed of Time, and hash of Time with secret
# key from the Rails session secret generated upon creation of project. Ensures
# the time cannot be modified by client.
def nonce(time = Time.now)
def nonce(secret_key, time = Time.now)
t = time.to_i
hashed = [t, secret_key]
digest = ::Digest::MD5.hexdigest(hashed.join(":"))
......@@ -292,21 +295,16 @@ def nonce(time = Time.now)
# Can be much shorter if the Stale directive is implemented. This would
# allow a user to use new nonce without prompting user again for their
# username and password.
def validate_nonce(request, value, seconds_to_timeout=5*60)
def validate_nonce(secret_key, request, value, seconds_to_timeout=5*60)
t = ActiveSupport::Base64.decode64(value).split(":").first.to_i
nonce(t) == value && (t - Time.now.to_i).abs <= seconds_to_timeout
nonce(secret_key, t) == value && (t - Time.now.to_i).abs <= seconds_to_timeout
end
# Opaque based on random generation - but changing each request?
def opaque()
def opaque(secret_key)
::Digest::MD5.hexdigest(secret_key)
end
# Set in /initializers/session_store.rb, and loaded even if sessions are not in use.
def secret_key
ActionController::Base.session_options[:secret]
end
end
end
end
......@@ -20,7 +20,7 @@ def process_action(action, *args)
:params => request.filtered_parameters,
:formats => request.formats.map(&:to_sym),
:method => request.method,
:path => (request.request_uri rescue "unknown")
:path => (request.fullpath rescue "unknown")
}
ActiveSupport::Notifications.instrument("action_controller.start_processing", raw_payload.dup)
......
......@@ -2,28 +2,29 @@ module ActionController #:nodoc:
module SessionManagement #:nodoc:
extend ActiveSupport::Concern
included do
# This is still needed for the session secret for some reason.
self.config.session_options ||= {}
end
def self.session_store_for(store)
case store
when :active_record_store
ActiveRecord::SessionStore
when Symbol
ActionDispatch::Session.const_get(store.to_s.camelize)
else
store
end
end
module ClassMethods
# Set the session store to be used for keeping the session data between requests.
# By default, sessions are stored in browser cookies (<tt>:cookie_store</tt>),
# but you can also specify one of the other included stores (<tt>:active_record_store</tt>,
# <tt>:mem_cache_store</tt>, or your own custom class.
def session_store=(store)
if store == :active_record_store
self.session_store = ActiveRecord::SessionStore
else
@@session_store = store.is_a?(Symbol) ?
ActionDispatch::Session.const_get(store.to_s.camelize) :
store
end
def session_options
config.session_options
end
# Returns the session store class currently used.
def session_store
if defined? @@session_store
@@session_store
else
ActionDispatch::Session::CookieStore
end
SessionManagement.session_store_for(config.session_store)
end
def session=(options = {})
......
......@@ -9,6 +9,10 @@ def url_options
super.reverse_merge(
:host => request.host_with_port,
:protocol => request.protocol,
# ROUTES TODO: relative_url_root should be middleware
# and the generator should take SCRIPT_NAME into
# consideration
:relative_url_root => config.relative_url_root,
:_path_segments => request.symbolized_path_parameters
)
end
......
......@@ -13,11 +13,28 @@ class Railtie < Rails::Railtie
log_subscriber ActionController::Railties::LogSubscriber.new
config.action_controller.session_store = :cookie_store
config.action_controller.session_options = {}
initializer "action_controller.logger" do
ActionController::Base.logger ||= Rails.logger
end
# assets_dir = defined?(Rails.public_path) ? Rails.public_path : "public"
# ActionView::DEFAULT_CONFIG = {
# :assets_dir => assets_dir,
# :javascripts_dir => "#{assets_dir}/javascripts",
# :stylesheets_dir => "#{assets_dir}/stylesheets",
# }
initializer "action_controller.set_configs" do |app|
paths = app.config.paths
ac = app.config.action_controller
ac.assets_dir = paths.public
ac.javascripts_dir = paths.public.javascripts
ac.stylesheets_dir = paths.public.stylesheets
app.config.action_controller.each do |k,v|
ActionController::Base.send "#{k}=", v
end
......
......@@ -322,6 +322,8 @@ def setup_controller_request_and_response
@controller ||= klass.new rescue nil
end
@request.env.delete('PATH_INFO')
if @controller
@controller.request = @request
@controller.params = {}
......@@ -333,15 +335,19 @@ def rescue_action_in_public!
@request.remote_addr = '208.77.188.166' # example.com
end
private
def build_request_uri(action, parameters)
unless @request.env['REQUEST_URI']
options = @controller.__send__(:url_options).merge(parameters)
options.update(:only_path => true, :action => action)
private
def build_request_uri(action, parameters)
unless @request.env["PATH_INFO"]
options = @controller.__send__(:url_options).merge(parameters)
options.update(:only_path => true, :action => action, :relative_url_root => nil)
rewriter = ActionController::UrlRewriter.new(@request, parameters)
url = ActionController::UrlRewriter.new(@request, parameters)
@request.request_uri = url.rewrite(@router, options)
end
url, query_string = rewriter.rewrite(@router, options).split("?", 2)
@request.env["SCRIPT_NAME"] = @controller.config.relative_url_root
@request.env["PATH_INFO"] = url
@request.env["QUERY_STRING"] = query_string || ""
end
end
end
end
......@@ -32,6 +32,7 @@ def self.rewrite(router, options, path_segments=nil)
# ROUTES TODO: Fix the tests
segments = options.delete(:_path_segments)
relative_url_root = options.delete(:relative_url_root).to_s
path_segments = path_segments ? path_segments.merge(segments || {}) : segments
unless options[:only_path]
......@@ -49,7 +50,8 @@ def self.rewrite(router, options, path_segments=nil)
path_options = yield(path_options) if block_given?
path = router.generate(path_options, path_segments || {})
rewritten_url << ActionController::Base.relative_url_root.to_s unless options[:skip_relative_url_root]
# ROUTES TODO: This can be called directly, so relative_url_root should probably be set in the router
rewritten_url << relative_url_root
rewritten_url << (options[:trailing_slash] ? path.sub(/\?|\z/) { "/" + $& } : path)
rewritten_url << "##{Rack::Utils.escape(options[:anchor].to_param.to_s)}" if options[:anchor]
......
......@@ -48,6 +48,7 @@ module ActionDispatch
autoload :Flash
autoload :Head
autoload :ParamsParser
autoload :RemoteIp
autoload :Rescue
autoload :ShowExceptions
autoload :Static
......
......@@ -119,36 +119,7 @@ def xml_http_request?
# delimited list in the case of multiple chained proxies; the last
# address which is not trusted is the originating IP.
def remote_ip
remote_addr_list = @env['REMOTE_ADDR'] && @env['REMOTE_ADDR'].scan(/[^,\s]+/)
unless remote_addr_list.blank?
not_trusted_addrs = remote_addr_list.reject {|addr| addr =~ TRUSTED_PROXIES || addr =~ ActionController::Base.trusted_proxies}
return not_trusted_addrs.first unless not_trusted_addrs.empty?
end
remote_ips = @env['HTTP_X_FORWARDED_FOR'] && @env['HTTP_X_FORWARDED_FOR'].split(',')
if @env.include? 'HTTP_CLIENT_IP'
if ActionController::Base.ip_spoofing_check && remote_ips && !remote_ips.include?(@env['HTTP_CLIENT_IP'])
# We don't know which came from the proxy, and which from the user
raise ActionController::ActionControllerError.new <<EOM
IP spoofing attack?!
HTTP_CLIENT_IP=#{@env['HTTP_CLIENT_IP'].inspect}
HTTP_X_FORWARDED_FOR=#{@env['HTTP_X_FORWARDED_FOR'].inspect}
EOM
end
return @env['HTTP_CLIENT_IP']
end
if remote_ips
while remote_ips.size > 1 && (TRUSTED_PROXIES =~ remote_ips.last.strip || ActionController::Base.trusted_proxies =~ remote_ips.last.strip)
remote_ips.pop
end
return remote_ips.last.strip
end
@env['REMOTE_ADDR']
(@env["action_dispatch.remote_ip"] || ip).to_s
end
# Returns the lowercase name of the HTTP server software.
......
......@@ -3,7 +3,7 @@ module Http
module URL
# Returns the complete URL used for this request.
def url
protocol + host_with_port + request_uri
protocol + host_with_port + fullpath
end
# Returns 'https://' if this is an SSL request and 'http://' otherwise.
......@@ -85,42 +85,11 @@ def subdomain(tld_length = 1)
subdomains(tld_length).join('.')
end
# Returns the query string, accounting for server idiosyncrasies.
def query_string
@env['QUERY_STRING'].present? ? @env['QUERY_STRING'] : (@env['REQUEST_URI'].to_s.split('?', 2)[1] || '')
end
# Returns the request URI, accounting for server idiosyncrasies.
# WEBrick includes the full URL. IIS leaves REQUEST_URI blank.
def request_uri
if uri = @env['REQUEST_URI']
# Remove domain, which webrick puts into the request_uri.
(%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri
else
# Construct IIS missing REQUEST_URI from SCRIPT_NAME and PATH_INFO.
uri = @env['PATH_INFO'].to_s
if script_filename = @env['SCRIPT_NAME'].to_s.match(%r{[^/]+$})
uri = uri.sub(/#{script_filename}\//, '')
end
env_qs = @env['QUERY_STRING'].to_s
uri += "?#{env_qs}" unless env_qs.empty?
if uri.blank?
@env.delete('REQUEST_URI')
else
@env['REQUEST_URI'] = uri
end
end
end
# Returns the interpreted \path to requested resource after all the installation
# directory of this application was taken into account.
def path
path = request_uri.to_s[/\A[^\?]*/]
path.sub!(/\A#{ActionController::Base.relative_url_root}/, '')
path
ActiveSupport::Deprecation.warn "Using #request_uri is deprecated. Use fullpath instead.", caller
fullpath
end
private
......
module ActionDispatch
class RemoteIp
class IpSpoofAttackError < StandardError ; end
class RemoteIpGetter
def initialize(env, check_ip_spoofing, trusted_proxies)
@env = env
@check_ip_spoofing = check_ip_spoofing
@trusted_proxies = trusted_proxies
end
def remote_addrs
@remote_addrs ||= begin
list = @env['REMOTE_ADDR'] ? @env['REMOTE_ADDR'].split(/[,\s]+/) : []
list.reject { |addr| addr =~ @trusted_proxies }
end
end
def to_s
return remote_addrs.first if remote_addrs.any?
forwarded_ips = @env['HTTP_X_FORWARDED_FOR'] ? @env['HTTP_X_FORWARDED_FOR'].strip.split(/[,\s]+/) : []
if client_ip = @env['HTTP_CLIENT_IP']
if @check_ip_spoofing && !forwarded_ips.include?(client_ip)
# We don't know which came from the proxy, and which from the user
raise IpSpoofAttackError, "IP spoofing attack?!" \
"HTTP_CLIENT_IP=#{@env['HTTP_CLIENT_IP'].inspect}" \
"HTTP_X_FORWARDED_FOR=#{@env['HTTP_X_FORWARDED_FOR'].inspect}"
end
return client_ip
end
return forwarded_ips.reject { |ip| ip =~ @trusted_proxies }.last || @env["REMOTE_ADDR"]
end
end
def initialize(app, check_ip_spoofing = true, trusted_proxies = nil)
@app = app
@check_ip_spoofing = check_ip_spoofing
regex = '(^127\.0\.0\.1$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\.)'
regex << "|(#{trusted_proxies})" if trusted_proxies
@trusted_proxies = Regexp.new(regex, "i")
end
def call(env)
env["action_dispatch.remote_ip"] = RemoteIpGetter.new(env, @check_ip_spoofing, @trusted_proxies)
@app.call(env)
end
end
end
\ No newline at end of file
......@@ -6,6 +6,7 @@ class Railtie < Rails::Railtie
railtie_name :action_dispatch
config.action_dispatch.x_sendfile_header = "X-Sendfile"
config.action_dispatch.ip_spoofing_check = true
# Prepare dispatcher callbacks and run 'prepare' callbacks
initializer "action_dispatch.prepare_dispatcher" do |app|
......
......@@ -175,15 +175,6 @@ module Subclasses
include Helpers, Rendering, Partials, ::ERB::Util
def config
self.config = DEFAULT_CONFIG unless @config
@config
end
def config=(config)
@config = ActiveSupport::OrderedOptions.new.merge(config)
end
extend ActiveSupport::Memoizable
attr_accessor :base_path, :assigns, :template_extension
......@@ -306,12 +297,13 @@ def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil,
@helpers = self.class.helpers || Module.new
@_controller = controller
@_config = ActiveSupport::InheritableOptions.new(controller.config) if controller
@_content_for = Hash.new {|h,k| h[k] = ActiveSupport::SafeBuffer.new }
@_virtual_path = nil
self.view_paths = view_paths
end
attr_internal :controller, :template
attr_internal :controller, :template, :config
attr_reader :view_paths
def view_paths=(paths)
......
......@@ -133,13 +133,6 @@ module Helpers #:nodoc:
# change. You can use something like Live HTTP Headers for Firefox to verify
# that the cache is indeed working.
module AssetTagHelper
assets_dir = defined?(Rails.public_path) ? Rails.public_path : "public"
ActionView::DEFAULT_CONFIG = {
:assets_dir => assets_dir,
:javascripts_dir => "#{assets_dir}/javascripts",
:stylesheets_dir => "#{assets_dir}/stylesheets",
}
JAVASCRIPT_DEFAULT_SOURCES = ['prototype', 'effects', 'dragdrop', 'controls', 'rails'].freeze unless const_defined?(:JAVASCRIPT_DEFAULT_SOURCES)
# Returns a link tag that browsers and news readers can use to auto-detect
......@@ -648,8 +641,8 @@ def compute_public_path(source, dir, ext = nil, include_host = true)
source = rewrite_asset_path(source)
if has_request && include_host
unless source =~ %r{^#{ActionController::Base.relative_url_root}/}
source = "#{ActionController::Base.relative_url_root}#{source}"
unless source =~ %r{^#{controller.config.relative_url_root}/}
source = "#{controller.config.relative_url_root}#{source}"
end
end
end
......
......@@ -114,7 +114,7 @@ def atom_feed(options = {}, &block)
feed_opts.merge!(options).reject!{|k,v| !k.to_s.match(/^xml/)}
xml.feed(feed_opts) do
xml.id(options[:id] || "tag:#{request.host},#{options[:schema_date]}:#{request.request_uri.split(".")[0]}")
xml.id(options[:id] || "tag:#{request.host},#{options[:schema_date]}:#{request.fullpath.split(".")[0]}")
xml.link(:rel => 'alternate', :type => 'text/html', :href => options[:root_url] || (request.protocol + request.host_with_port))
xml.link(:rel => 'self', :type => 'application/atom+xml', :href => options[:url] || request.url)
......
......@@ -544,10 +544,11 @@ def current_page?(options)
# submitted url doesn't have any either. This lets the function
# work with things like ?order=asc
if url_string.index("?")
request_uri = request.request_uri
request_uri = request.fullpath
else
request_uri = request.request_uri.split('?').first
request_uri = request.path
end
if url_string =~ /^\w+:\/\//
url_string == "#{request.protocol}#{request.host_with_port}#{request_uri}"
else
......
......@@ -34,6 +34,8 @@ def initialize
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@request.env.delete('PATH_INFO')
@params = {}
end
end
......@@ -60,6 +62,10 @@ def setup_with_controller
make_test_case_available_to_view!
end
def config
@controller.config
end
def render(options = {}, local_assigns = {}, &block)
@rendered << output = _view.render(options, local_assigns, &block)
output
......
......@@ -105,6 +105,21 @@ def call(env)
end
end
class BasicController
attr_accessor :request
def config
@config ||= ActiveSupport::InheritableOptions.new(ActionController::Base.config).tap do |config|
# VIEW TODO: View tests should not require a controller
public_dir = File.expand_path("../fixtures/public", __FILE__)
config.assets_dir = public_dir
config.javascripts_dir = "#{public_dir}/javascripts"
config.stylesheets_dir = "#{public_dir}/stylesheets"
config
end
end
end
class ActionController::IntegrationTest < ActiveSupport::TestCase
def self.build_app(routes = nil)
RoutedRackApp.new(routes || ActionDispatch::Routing::RouteSet.new) do |middleware|
......
......@@ -51,6 +51,7 @@ def setup
@request = ActionController::TestRequest.new
@request.host = 'hostname.com'
@request.env.delete('PATH_INFO')
@response = ActionController::TestResponse.new
@controller = PageCachingTestController.new
......@@ -110,7 +111,7 @@ def test_should_cache_with_trailing_slash_on_url
end
def test_should_cache_ok_at_custom_path
@request.request_uri = "/index.html"
@request.env['PATH_INFO'] = '/index.html'
get :ok
assert_response :ok
assert File.exist?("#{FILE_STORE_PATH}/index.html")
......@@ -305,12 +306,9 @@ def test_action_cache_with_layout_and_layout_cache_false
end
def test_action_cache_conditional_options
old_use_accept_header = ActionController::Base.use_accept_header
ActionController::Base.use_accept_header = true
@request.env['HTTP_ACCEPT'] = 'application/json'
get :index
assert !fragment_exist?('hostname.com/action_caching_test')
ActionController::Base.use_accept_header = old_use_accept_header
end
def test_action_cache_with_store_options
......
......@@ -145,18 +145,6 @@ def test_change_for_rxml
class AcceptBasedContentTypeTest < ActionController::TestCase
tests OldContentTypeController
def setup
super
@_old_accept_header = ActionController::Base.use_accept_header
ActionController::Base.use_accept_header = true
end
def teardown
super
ActionController::Base.use_accept_header = @_old_accept_header
end
def test_render_default_content_types_for_respond_to
@request.accept = Mime::HTML.to_s
get :render_default_content_types_for_respond_to
......
......@@ -40,7 +40,8 @@ def authenticate_with_request
setup do
# Used as secret in generating nonce to prevent tampering of timestamp
@old_secret, ActionController::Base.session_options[:secret] = ActionController::Base.session_options[:secret], "session_options_secret"
@secret = "session_options_secret"
@old_secret, ActionController::Base.session_options[:secret] = ActionController::Base.session_options[:secret], @secret
end
teardown do
......@@ -138,7 +139,7 @@ def authenticate_with_request
test "authentication request with request-uri that doesn't match credentials digest-uri" do
@request.env['HTTP_AUTHORIZATION'] = encode_credentials(:username => 'pretty', :password => 'please')
@request.env['REQUEST_URI'] = "/http_digest_authentication_test/dummy_digest/altered/uri"
@request.env['PATH_INFO'] = "/http_digest_authentication_test/dummy_digest/altered/uri"
get :display
assert_response :unauthorized
......@@ -147,7 +148,8 @@ def authenticate_with_request
test "authentication request with absolute request uri (as in webrick)" do
@request.env['HTTP_AUTHORIZATION'] = encode_credentials(:username => 'pretty', :password => 'please')
@request.env['REQUEST_URI'] = "http://test.host/http_digest_authentication_test/dummy_digest"
@request.env["SERVER_NAME"] = "test.host"
@request.env['PATH_INFO'] = "/http_digest_authentication_test/dummy_digest"
get :display
......@@ -170,7 +172,8 @@ def authenticate_with_request
test "authentication request with absolute uri in both request and credentials (as in Webrick with IE)" do
@request.env['HTTP_AUTHORIZATION'] = encode_credentials(:url => "http://test.host/http_digest_authentication_test/dummy_digest",
:username => 'pretty', :password => 'please')
@request.env['REQUEST_URI'] = "http://test.host/http_digest_authentication_test/dummy_digest"
@request.env['SERVER_NAME'] = "test.host"
@request.env['PATH_INFO'] = "/http_digest_authentication_test/dummy_digest"
get :display
......@@ -202,7 +205,7 @@ def authenticate_with_request
test "validate_digest_response should fail with nil returning password_procedure" do
@request.env['HTTP_AUTHORIZATION'] = encode_credentials(:username => nil, :password => nil)
assert !ActionController::HttpAuthentication::Digest.validate_digest_response(@request, "SuperSecret"){nil}
assert !ActionController::HttpAuthentication::Digest.validate_digest_response(@secret, @request, "SuperSecret"){nil}
end
private
......@@ -225,7 +228,7 @@ def encode_credentials(options)
credentials = decode_credentials(@response.headers['WWW-Authenticate'])
credentials.merge!(options)
credentials.merge!(:uri => @request.env['REQUEST_URI'].to_s)
credentials.merge!(:uri => @request.env['PATH_INFO'].to_s)
ActionController::HttpAuthentication::Digest.encode_credentials(method, credentials, password, options[:password_is_ha1])
end
......
......@@ -333,7 +333,7 @@ def test_get_with_query_string
with_test_route_set do
get '/get_with_params?foo=bar'
assert_equal '/get_with_params?foo=bar', request.env["REQUEST_URI"]
assert_equal '/get_with_params?foo=bar', request.request_uri
assert_equal '/get_with_params?foo=bar', request.fullpath
assert_equal "foo=bar", request.env["QUERY_STRING"]
assert_equal 'foo=bar', request.query_string
assert_equal 'bar', request.parameters['foo']
......@@ -346,8 +346,8 @@ def test_get_with_query_string
def test_get_with_parameters
with_test_route_set do
get '/get_with_params', :foo => "bar"
assert_equal '/get_with_params', request.env["REQUEST_URI"]
assert_equal '/get_with_params', request.request_uri
assert_equal '/get_with_params', request.env["PATH_INFO"]
assert_equal '/get_with_params', request.path_info
assert_equal 'foo=bar', request.env["QUERY_STRING"]
assert_equal 'foo=bar', request.query_string
assert_equal 'bar', request.parameters['foo']
......
......@@ -155,13 +155,11 @@ class RespondToControllerTest < ActionController::TestCase
def setup
super
ActionController::Base.use_accept_header = true
@request.host = "www.example.com"
end
def teardown
super
ActionController::Base.use_accept_header = false
end
def test_html
......@@ -544,13 +542,11 @@ class RespondWithControllerTest < ActionController::TestCase
def setup
super
ActionController::Base.use_accept_header = true
@request.host = "www.example.com"
end
def teardown
super
ActionController::Base.use_accept_header = false
end
def test_using_resource
......
......@@ -42,7 +42,7 @@ def test_params
end
def test_uri
render :text => request.request_uri
render :text => request.fullpath
end
def test_query_string
......@@ -128,6 +128,7 @@ def setup
@controller = TestController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@request.env['PATH_INFO'] = nil
end
def test_raw_post_handling
......@@ -199,7 +200,7 @@ def test_process_with_request_uri_with_params
end
def test_process_with_request_uri_with_params_with_explicit_uri
@request.request_uri = "/explicit/uri"
@request.env['PATH_INFO'] = "/explicit/uri"
process :test_uri, :id => 7
assert_equal "/explicit/uri", @response.body
end
......@@ -210,7 +211,8 @@ def test_process_with_query_string
end
def test_process_with_query_string_with_explicit_uri
@request.request_uri = "/explicit/uri?q=test?extra=question"
@request.env['PATH_INFO'] = '/explicit/uri'
@request.env['QUERY_STRING'] = 'q=test?extra=question'
process :test_query_string
assert_equal "q=test?extra=question", @response.body
end
......
......@@ -113,15 +113,13 @@ def test_trailing_slash_with_params
end
def test_relative_url_root_is_respected
orig_relative_url_root = ActionController::Base.relative_url_root
ActionController::Base.relative_url_root = '/subdir'
# ROUTES TODO: Tests should not have to pass :relative_url_root directly. This
# should probably come from the router.
add_host!
assert_equal('https://www.basecamphq.com/subdir/c/a/i',
W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https')
W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => 'https', :relative_url_root => '/subdir')
)
ensure
ActionController::Base.relative_url_root = orig_relative_url_root
end
def test_named_routes
......@@ -146,9 +144,6 @@ def test_named_routes
end
def test_relative_url_root_is_respected_for_named_routes
orig_relative_url_root = ActionController::Base.relative_url_root
ActionController::Base.relative_url_root = '/subdir'
with_routing do |set|
set.draw do |map|
match '/home/sweet/home/:user', :to => 'home#index', :as => :home
......@@ -158,10 +153,8 @@ def test_relative_url_root_is_respected_for_named_routes
controller = kls.new
assert_equal 'http://www.basecamphq.com/subdir/home/sweet/home/again',
controller.send(:home_url, :host => 'www.basecamphq.com', :user => 'again')
controller.send(:home_url, :host => 'www.basecamphq.com', :user => 'again', :relative_url_root => "/subdir")
end
ensure
ActionController::Base.relative_url_root = orig_relative_url_root
end
def test_only_path
......
require 'abstract_unit'
class RequestTest < ActiveSupport::TestCase
def setup
ActionController::Base.relative_url_root = nil
end
def teardown
ActionController::Base.relative_url_root = nil
end
test "remote ip" do
request = stub_request 'REMOTE_ADDR' => '1.2.3.4'
assert_equal '1.2.3.4', request.remote_ip
......@@ -50,7 +42,7 @@ def teardown
request = stub_request 'HTTP_X_FORWARDED_FOR' => '1.1.1.1',
'HTTP_CLIENT_IP' => '2.2.2.2'
e = assert_raise(ActionController::ActionControllerError) {
e = assert_raise(ActionDispatch::RemoteIp::IpSpoofAttackError) {
request.remote_ip
}
assert_match /IP spoofing attack/, e.message
......@@ -62,18 +54,17 @@ def teardown
# example is WAP. Since the cellular network is not IP based, it's a
# leap of faith to assume that their proxies are ever going to set the
# HTTP_CLIENT_IP/HTTP_X_FORWARDED_FOR headers properly.
ActionController::Base.ip_spoofing_check = false
request = stub_request 'HTTP_X_FORWARDED_FOR' => '1.1.1.1',
'HTTP_CLIENT_IP' => '2.2.2.2'
'HTTP_CLIENT_IP' => '2.2.2.2',
:ip_spoofing_check => false
assert_equal '2.2.2.2', request.remote_ip
ActionController::Base.ip_spoofing_check = true
request = stub_request 'HTTP_X_FORWARDED_FOR' => '8.8.8.8, 9.9.9.9'
assert_equal '9.9.9.9', request.remote_ip
end
test "remote ip with user specified trusted proxies" do
ActionController::Base.trusted_proxies = /^67\.205\.106\.73$/i
@trusted_proxies = /^67\.205\.106\.73$/i
request = stub_request 'REMOTE_ADDR' => '67.205.106.73',
'HTTP_X_FORWARDED_FOR' => '3.4.5.6'
......@@ -96,8 +87,6 @@ def teardown
request = stub_request 'HTTP_X_FORWARDED_FOR' => '9.9.9.9, 3.4.5.6, 10.0.0.1, 67.205.106.73'
assert_equal '3.4.5.6', request.remote_ip
ActionController::Base.trusted_proxies = nil
end
test "domains" do
......@@ -151,104 +140,34 @@ def teardown
assert_equal ":8080", request.port_string
end
test "request uri" do
request = stub_request 'REQUEST_URI' => "http://www.rubyonrails.org/path/of/some/uri?mapped=1"
assert_equal "/path/of/some/uri?mapped=1", request.request_uri
assert_equal "/path/of/some/uri", request.path
request = stub_request 'REQUEST_URI' => "http://www.rubyonrails.org/path/of/some/uri"
assert_equal "/path/of/some/uri", request.request_uri
assert_equal "/path/of/some/uri", request.path
request = stub_request 'REQUEST_URI' => "/path/of/some/uri"
assert_equal "/path/of/some/uri", request.request_uri
assert_equal "/path/of/some/uri", request.path
request = stub_request 'REQUEST_URI' => "/"
assert_equal "/", request.request_uri
assert_equal "/", request.path
request = stub_request 'REQUEST_URI' => "/?m=b"
assert_equal "/?m=b", request.request_uri
assert_equal "/", request.path
request = stub_request 'REQUEST_URI' => "/", 'SCRIPT_NAME' => '/dispatch.cgi'
assert_equal "/", request.request_uri
assert_equal "/", request.path
ActionController::Base.relative_url_root = "/hieraki"
request = stub_request 'REQUEST_URI' => "/hieraki/", 'SCRIPT_NAME' => "/hieraki/dispatch.cgi"
assert_equal "/hieraki/", request.request_uri
assert_equal "/", request.path
ActionController::Base.relative_url_root = nil
ActionController::Base.relative_url_root = "/collaboration/hieraki"
request = stub_request 'REQUEST_URI' => "/collaboration/hieraki/books/edit/2",
'SCRIPT_NAME' => "/collaboration/hieraki/dispatch.cgi"
assert_equal "/collaboration/hieraki/books/edit/2", request.request_uri
assert_equal "/books/edit/2", request.path
ActionController::Base.relative_url_root = nil
# The following tests are for when REQUEST_URI is not supplied (as in IIS)
request = stub_request 'PATH_INFO' => "/path/of/some/uri?mapped=1",
'SCRIPT_NAME' => nil,
'REQUEST_URI' => nil
assert_equal "/path/of/some/uri?mapped=1", request.request_uri
assert_equal "/path/of/some/uri", request.path
ActionController::Base.relative_url_root = '/path'
request = stub_request 'PATH_INFO' => "/path/of/some/uri?mapped=1",
'SCRIPT_NAME' => "/path/dispatch.rb",
'REQUEST_URI' => nil
assert_equal "/path/of/some/uri?mapped=1", request.request_uri
assert_equal "/of/some/uri", request.path
ActionController::Base.relative_url_root = nil
request = stub_request 'PATH_INFO' => "/path/of/some/uri",
'SCRIPT_NAME' => nil,
'REQUEST_URI' => nil
assert_equal "/path/of/some/uri", request.request_uri
assert_equal "/path/of/some/uri", request.path
request = stub_request 'PATH_INFO' => '/', 'REQUEST_URI' => nil
assert_equal "/", request.request_uri
assert_equal "/", request.path
request = stub_request 'PATH_INFO' => '/?m=b', 'REQUEST_URI' => nil
assert_equal "/?m=b", request.request_uri
assert_equal "/", request.path
request = stub_request 'PATH_INFO' => "/",
'SCRIPT_NAME' => "/dispatch.cgi",
'REQUEST_URI' => nil
assert_equal "/", request.request_uri
assert_equal "/", request.path
ActionController::Base.relative_url_root = '/hieraki'
request = stub_request 'PATH_INFO' => "/hieraki/",
'SCRIPT_NAME' => "/hieraki/dispatch.cgi",
'REQUEST_URI' => nil
assert_equal "/hieraki/", request.request_uri
assert_equal "/", request.path
ActionController::Base.relative_url_root = nil
request = stub_request 'REQUEST_URI' => '/hieraki/dispatch.cgi'
ActionController::Base.relative_url_root = '/hieraki'
assert_equal "/dispatch.cgi", request.path
ActionController::Base.relative_url_root = nil
request = stub_request 'REQUEST_URI' => '/hieraki/dispatch.cgi'
ActionController::Base.relative_url_root = '/foo'
assert_equal "/hieraki/dispatch.cgi", request.path
ActionController::Base.relative_url_root = nil
# This test ensures that Rails uses REQUEST_URI over PATH_INFO
ActionController::Base.relative_url_root = nil
request = stub_request 'REQUEST_URI' => "/some/path",
'PATH_INFO' => "/another/path",
'SCRIPT_NAME' => "/dispatch.cgi"
assert_equal "/some/path", request.request_uri
assert_equal "/some/path", request.path
test "full path" do
request = stub_request 'SCRIPT_NAME' => '', 'PATH_INFO' => '/path/of/some/uri', 'QUERY_STRING' => 'mapped=1'
assert_equal "/path/of/some/uri?mapped=1", request.fullpath
assert_equal "/path/of/some/uri", request.path_info
request = stub_request 'SCRIPT_NAME' => '', 'PATH_INFO' => '/path/of/some/uri'
assert_equal "/path/of/some/uri", request.fullpath
assert_equal "/path/of/some/uri", request.path_info
request = stub_request 'SCRIPT_NAME' => '', 'PATH_INFO' => '/'
assert_equal "/", request.fullpath
assert_equal "/", request.path_info
request = stub_request 'SCRIPT_NAME' => '', 'PATH_INFO' => '/', 'QUERY_STRING' => 'm=b'
assert_equal "/?m=b", request.fullpath
assert_equal "/", request.path_info
request = stub_request 'SCRIPT_NAME' => '/hieraki', 'PATH_INFO' => '/'
assert_equal "/hieraki/", request.fullpath
assert_equal "/", request.path_info
request = stub_request 'SCRIPT_NAME' => '/collaboration/hieraki', 'PATH_INFO' => '/books/edit/2'
assert_equal "/collaboration/hieraki/books/edit/2", request.fullpath
assert_equal "/books/edit/2", request.path_info
request = stub_request 'SCRIPT_NAME' => '/path', 'PATH_INFO' => '/of/some/uri', 'QUERY_STRING' => 'mapped=1'
assert_equal "/path/of/some/uri?mapped=1", request.fullpath
assert_equal "/of/some/uri", request.path_info
end
......@@ -506,18 +425,14 @@ def teardown
protected
def stub_request(env={})
def stub_request(env = {})
ip_spoofing_check = env.key?(:ip_spoofing_check) ? env.delete(:ip_spoofing_check) : true
ip_app = ActionDispatch::RemoteIp.new(Proc.new { }, ip_spoofing_check, @trusted_proxies)
ip_app.call(env)
ActionDispatch::Request.new(env)
end
def with_set(*args)
args
end
def with_accept_header(value)
ActionController::Base.use_accept_header, old = value, ActionController::Base.use_accept_header
yield
ensure
ActionController::Base.use_accept_header = old
end
end
require 'abstract_unit'
require 'active_support/ordered_options'
class AssetTagHelperTest < ActionView::TestCase
tests ActionView::Helpers::AssetTagHelper
class FakeController
attr_accessor :request
DEFAULT_CONFIG = ActionView::DEFAULT_CONFIG.merge(
:assets_dir => File.dirname(__FILE__) + "/../fixtures/public",
:javascripts_dir => File.dirname(__FILE__) + "/../fixtures/public/javascripts",
:stylesheets_dir => File.dirname(__FILE__) + "/../fixtures/public/stylesheets")
def config
@config ||= ActiveSupport::InheritableOptions.new(ActionController::Base.config)
end
end
include ActiveSupport::Configurable
class AssetTagHelperTest < ActionView::TestCase
tests ActionView::Helpers::AssetTagHelper
def setup
super
......@@ -32,8 +34,7 @@ def setup
)
end
@controller = Class.new do
attr_accessor :request
@controller = Class.new(BasicController) do
def url_for(*args) "http://www.example.com" end
end.new
......@@ -372,11 +373,9 @@ def test_timebased_asset_id
end
def test_timebased_asset_id_with_relative_url_root
ActionController::Base.relative_url_root = "/collaboration/hieraki"
expected_time = File.stat(File.expand_path(File.dirname(__FILE__) + "/../fixtures/public/images/rails.png")).mtime.to_i.to_s
assert_equal %(<img alt="Rails" src="#{ActionController::Base.relative_url_root}/images/rails.png?#{expected_time}" />), image_tag("rails.png")
ensure
ActionController::Base.relative_url_root = ""
@controller.config.relative_url_root = "/collaboration/hieraki"
expected_time = File.stat(File.expand_path(File.dirname(__FILE__) + "/../fixtures/public/images/rails.png")).mtime.to_i.to_s
assert_equal %(<img alt="Rails" src="#{@controller.config.relative_url_root}/images/rails.png?#{expected_time}" />), image_tag("rails.png")
end
def test_should_skip_asset_id_on_complete_url
......@@ -606,7 +605,7 @@ def test_caching_javascript_include_tag_with_all_puts_defaults_at_the_start_of_t
def test_caching_javascript_include_tag_with_relative_url_root
ENV["RAILS_ASSET_ID"] = ""
ActionController::Base.relative_url_root = "/collaboration/hieraki"
@controller.config.relative_url_root = "/collaboration/hieraki"
ActionController::Base.perform_caching = true
assert_dom_equal(
......@@ -624,7 +623,6 @@ def test_caching_javascript_include_tag_with_relative_url_root
assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js'))
ensure
ActionController::Base.relative_url_root = nil
FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js'))
FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js'))
end
......@@ -821,7 +819,7 @@ def test_caching_stylesheet_link_tag_when_caching_on_with_proc_asset_host
def test_caching_stylesheet_link_tag_with_relative_url_root
ENV["RAILS_ASSET_ID"] = ""
ActionController::Base.relative_url_root = "/collaboration/hieraki"
@controller.config.relative_url_root = "/collaboration/hieraki"
ActionController::Base.perform_caching = true
assert_dom_equal(
......@@ -841,7 +839,6 @@ def test_caching_stylesheet_link_tag_with_relative_url_root
assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
ensure
ActionController::Base.relative_url_root = nil
FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css'))
FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
end
......@@ -879,21 +876,16 @@ def test_caching_stylesheet_include_tag_when_caching_off
class AssetTagHelperNonVhostTest < ActionView::TestCase
tests ActionView::Helpers::AssetTagHelper
DEFAULT_CONFIG = ActionView::DEFAULT_CONFIG
include ActiveSupport::Configurable
def setup
super
ActionController::Base.relative_url_root = "/collaboration/hieraki"
@controller = Class.new do
attr_accessor :request
@controller = Class.new(BasicController) do
def url_for(options)
"http://www.example.com/collaboration/hieraki"
end
end.new
@controller.config.relative_url_root = "/collaboration/hieraki"
@request = Class.new do
def protocol
'gopher://'
......@@ -905,10 +897,6 @@ def protocol
ActionView::Helpers::AssetTagHelper::reset_javascript_include_default
end
def teardown
ActionController::Base.relative_url_root = nil
end
def test_should_compute_proper_path
assert_dom_equal(%(<link href="http://www.example.com/collaboration/hieraki" rel="alternate" title="RSS" type="application/rss+xml" />), auto_discovery_link_tag)
assert_dom_equal(%(/collaboration/hieraki/javascripts/xmlhr.js), javascript_path("xmlhr"))
......
......@@ -3,17 +3,16 @@
class FormTagHelperTest < ActionView::TestCase
tests ActionView::Helpers::FormTagHelper
include ActiveSupport::Configurable
DEFAULT_CONFIG = ActionView::DEFAULT_CONFIG
# include ActiveSupport::Configurable
# DEFAULT_CONFIG = ActionView::DEFAULT_CONFIG
def setup
super
@controller = Class.new do
@controller = Class.new(BasicController) do
def url_for(options)
"http://www.example.com"
end
end
@controller = @controller.new
end.new
end
VALID_HTML_ID = /^[A-Za-z][-_:.A-Za-z0-9]*$/ # see http://www.w3.org/TR/html4/types.html#type-name
......
# encoding: utf-8
require 'abstract_unit'
require 'active_support/ordered_options'
require 'controller/fake_controllers'
RequestMock = Struct.new("Request", :request_uri, :protocol, :host_with_port, :env)
class UrlHelperTest < ActionView::TestCase
include ActiveSupport::Configurable
DEFAULT_CONFIG = ActionView::DEFAULT_CONFIG
def setup
super
@controller = Class.new do
attr_accessor :url, :request
@controller = Class.new(BasicController) do
attr_accessor :url
def url_for(options)
url
end
end
@controller = @controller.new
@request = @controller.request = ActionDispatch::TestRequest.new
@controller.url = "http://www.example.com"
end
......@@ -38,12 +37,13 @@ def test_url_for_escapes_url_once
end
def test_url_for_with_back
@controller.request = RequestMock.new("http://www.example.com/weblog/show", nil, nil, {'HTTP_REFERER' => 'http://www.example.com/referer'})
@request.env['HTTP_REFERER'] = 'http://www.example.com/referer'
assert_equal 'http://www.example.com/referer', url_for(:back)
end
def test_url_for_with_back_and_no_referer
@controller.request = RequestMock.new("http://www.example.com/weblog/show", nil, nil, {})
@request.env['HOST_NAME'] = 'www.example.com'
@request.env['PATH_INFO'] = '/weblog/show'
assert_equal 'javascript:history.back()', url_for(:back)
end
......@@ -144,22 +144,28 @@ def test_link_tag_with_query_and_no_name
end
def test_link_tag_with_back
@controller.request = RequestMock.new("http://www.example.com/weblog/show", nil, nil, {'HTTP_REFERER' => 'http://www.example.com/referer'})
@request.env['HOST_NAME'] = 'www.example.com'
@request.env['PATH_INFO'] = '/weblog/show'
@request.env['HTTP_REFERER'] = 'http://www.example.com/referer'
assert_dom_equal "<a href=\"http://www.example.com/referer\">go back</a>", link_to('go back', :back)
end
def test_link_tag_with_back_and_no_referer
@controller.request = RequestMock.new("http://www.example.com/weblog/show", nil, nil, {})
@request.env['HOST_NAME'] = 'www.example.com'
@request.env['PATH_INFO'] = '/weblog/show'
assert_dom_equal "<a href=\"javascript:history.back()\">go back</a>", link_to('go back', :back)
end
def test_link_tag_with_back
@controller.request = RequestMock.new("http://www.example.com/weblog/show", nil, nil, {'HTTP_REFERER' => 'http://www.example.com/referer'})
@request.env['HOST_NAME'] = 'www.example.com'
@request.env['PATH_INFO'] = '/weblog/show'
@request.env['HTTP_REFERER'] = 'http://www.example.com/referer'
assert_dom_equal "<a href=\"http://www.example.com/referer\">go back</a>", link_to('go back', :back)
end
def test_link_tag_with_back_and_no_referer
@controller.request = RequestMock.new("http://www.example.com/weblog/show", nil, nil, {})
@request.env['HOST_NAME'] = 'www.example.com'
@request.env['PATH_INFO'] = '/weblog/show'
assert_dom_equal "<a href=\"javascript:history.back()\">go back</a>", link_to('go back', :back)
end
......@@ -263,55 +269,60 @@ def test_link_to_if
end
def test_current_page_with_simple_url
@controller.request = RequestMock.new("http://www.example.com/weblog/show")
@request.env['HTTP_HOST'] = 'www.example.com'
@request.env['PATH_INFO'] = '/weblog/show'
@controller.url = "http://www.example.com/weblog/show"
assert current_page?({ :action => "show", :controller => "weblog" })
assert current_page?("http://www.example.com/weblog/show")
end
def test_current_page_ignoring_params
@controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc&page=1")
@request.env['HTTP_HOST'] = 'www.example.com'
@request.env['PATH_INFO'] = '/weblog/show'
@request.env['QUERY_STRING'] = 'order=desc&page=1'
@controller.url = "http://www.example.com/weblog/show?order=desc&page=1"
assert current_page?({ :action => "show", :controller => "weblog" })
assert current_page?("http://www.example.com/weblog/show")
end
def test_current_page_with_params_that_match
@controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc&page=1")
@request.env['HTTP_HOST'] = 'www.example.com'
@request.env['PATH_INFO'] = '/weblog/show'
@request.env['QUERY_STRING'] = 'order=desc&page=1'
@controller.url = "http://www.example.com/weblog/show?order=desc&page=1"
assert current_page?({ :action => "show", :controller => "weblog", :order => "desc", :page => "1" })
assert current_page?("http://www.example.com/weblog/show?order=desc&amp;page=1")
end
def test_link_unless_current
@controller.request = RequestMock.new("http://www.example.com/weblog/show")
@request.env['HTTP_HOST'] = 'www.example.com'
@request.env['PATH_INFO'] = '/weblog/show'
@controller.url = "http://www.example.com/weblog/show"
assert_equal "Showing", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" })
assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show")
@controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc")
@request.env['QUERY_STRING'] = 'order=desc'
@controller.url = "http://www.example.com/weblog/show"
assert_equal "Showing", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" })
assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show")
@controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc&page=1")
@request.env['QUERY_STRING'] = 'order=desc&page=1'
@controller.url = "http://www.example.com/weblog/show?order=desc&page=1"
assert_equal "Showing", link_to_unless_current("Showing", { :action => "show", :controller => "weblog", :order=>'desc', :page=>'1' })
assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show?order=desc&page=1")
assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show?order=desc&page=1")
@controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc")
@request.env['QUERY_STRING'] = 'order=desc'
@controller.url = "http://www.example.com/weblog/show?order=asc"
assert_equal "<a href=\"http://www.example.com/weblog/show?order=asc\">Showing</a>", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" })
assert_equal "<a href=\"http://www.example.com/weblog/show?order=asc\">Showing</a>", link_to_unless_current("Showing", "http://www.example.com/weblog/show?order=asc")
@controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc&page=1")
@request.env['QUERY_STRING'] = 'order=desc&page=1'
@controller.url = "http://www.example.com/weblog/show?order=desc&page=2"
assert_equal "<a href=\"http://www.example.com/weblog/show?order=desc&page=2\">Showing</a>", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" })
assert_equal "<a href=\"http://www.example.com/weblog/show?order=desc&amp;page=2\">Showing</a>", link_to_unless_current("Showing", "http://www.example.com/weblog/show?order=desc&page=2")
@controller.request = RequestMock.new("http://www.example.com/weblog/show")
@request.env['QUERY_STRING'] = ''
@controller.url = "http://www.example.com/weblog/list"
assert_equal "<a href=\"http://www.example.com/weblog/list\">Listing</a>",
link_to_unless_current("Listing", :action => "list", :controller => "weblog")
......@@ -464,8 +475,6 @@ def render_default
class LinkToUnlessCurrentWithControllerTest < ActionController::TestCase
def setup
super
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@controller = TasksController.new
end
......@@ -565,7 +574,6 @@ def rescue_action(e) raise e end
class PolymorphicControllerTest < ActionController::TestCase
def setup
super
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
......
......@@ -18,4 +18,10 @@ def method_missing(name, *args)
end
end
end
class InheritableOptions < OrderedOptions
def initialize(parent)
super() { |h,k| parent[k] }
end
end
end
......@@ -84,11 +84,12 @@ def default_middleware
middleware.use('::Rack::Runtime')
middleware.use('::Rails::Rack::Logger')
middleware.use('::ActionDispatch::ShowExceptions', lambda { consider_all_requests_local })
middleware.use("::ActionDispatch::RemoteIp", lambda { action_dispatch.ip_spoofing_check }, lambda { action_dispatch.trusted_proxies })
middleware.use('::Rack::Sendfile', lambda { action_dispatch.x_sendfile_header })
middleware.use('::ActionDispatch::Callbacks', lambda { !cache_classes })
middleware.use('::ActionDispatch::Cookies')
middleware.use(lambda { ActionController::Base.session_store }, lambda { ActionController::Base.session_options })
middleware.use('::ActionDispatch::Flash', :if => lambda { ActionController::Base.session_store })
middleware.use(lambda { ActionController::SessionManagement.session_store_for(action_controller.session_store) }, lambda { action_controller.session })
middleware.use('::ActionDispatch::Flash', :if => lambda { action_controller.session_store })
middleware.use(lambda { metal_loader.build_middleware(metals) }, :if => lambda { metal_loader.metals.any? })
middleware.use('ActionDispatch::ParamsParser')
middleware.use('::Rack::MethodOverride')
......
......@@ -26,6 +26,9 @@ def paths
paths.config.initializers "config/initializers", :glob => "**/*.rb"
paths.config.locales "config/locales", :glob => "*.{rb,yml}"
paths.config.routes "config/routes.rb"
paths.public "public"
paths.public.javascripts "public/javascripts"
paths.public.stylesheets "public/stylesheets"
paths
end
end
......
......@@ -87,6 +87,7 @@ def logger
%w(info debug warn error fatal unknown).each do |level|
class_eval <<-METHOD, __FILE__, __LINE__ + 1
def #{level}(*args, &block)
return unless logger
logger.#{level}(*args, &block)
end
METHOD
......
......@@ -19,7 +19,7 @@ def call(env)
def before_dispatch(env)
request = ActionDispatch::Request.new(env)
path = request.request_uri.inspect rescue "unknown"
path = request.fullpath.inspect rescue "unknown"
info "\n\nStarted #{request.method.to_s.upcase} #{path} " <<
"for #{request.remote_ip} at #{Time.now.to_s(:db)}"
......
......@@ -28,7 +28,7 @@ def self.call(env)
end
RUBY
get "/"
get "/not/slash"
assert_equal 200, last_response.status
assert_equal "FooMetal", last_response.body
end
......@@ -50,7 +50,7 @@ def self.call(env)
end
RUBY
get "/"
get "/not/slash"
assert_equal 200, last_response.status
assert_equal "Metal B", last_response.body
end
......
require 'isolation/abstract_unit'
class MiddlewareStackDefaultsTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def setup
boot_rails
require "rails"
require "action_controller/railtie"
Object.const_set(:MyApplication, Class.new(Rails::Application))
MyApplication.class_eval do
config.action_controller.session = { :key => "_myapp_session", :secret => "OMG A SEKRET" * 10 }
end
end
def remote_ip(env = {})
remote_ip = nil
env = Rack::MockRequest.env_for("/").merge(env).merge('action_dispatch.show_exceptions' => false)
endpoint = Proc.new do |e|
remote_ip = ActionDispatch::Request.new(e).remote_ip
[200, {}, ["Hello"]]
end
out = MyApplication.middleware.build(endpoint).call(env)
remote_ip
end
test "remote_ip works" do
assert_equal "1.1.1.1", remote_ip("REMOTE_ADDR" => "1.1.1.1")
end
test "checks IP spoofing by default" do
assert_raises(ActionDispatch::RemoteIp::IpSpoofAttackError) do
remote_ip("HTTP_X_FORWARDED_FOR" => "1.1.1.1", "HTTP_CLIENT_IP" => "1.1.1.2")
end
end
test "can disable IP spoofing check" do
MyApplication.config.action_dispatch.ip_spoofing_check = false
assert_nothing_raised(ActionDispatch::RemoteIp::IpSpoofAttackError) do
assert_equal "1.1.1.2", remote_ip("HTTP_X_FORWARDED_FOR" => "1.1.1.1", "HTTP_CLIENT_IP" => "1.1.1.2")
end
end
test "the user can set trusted proxies" do
MyApplication.config.action_dispatch.trusted_proxies = /^4\.2\.42\.42$/
assert_equal "1.1.1.1", remote_ip("REMOTE_ADDR" => "4.2.42.42,1.1.1.1")
end
end
......@@ -19,6 +19,7 @@ def setup
"Rack::Runtime",
"Rails::Rack::Logger",
"ActionDispatch::ShowExceptions",
"ActionDispatch::RemoteIp",
"Rack::Sendfile",
"ActionDispatch::Callbacks",
"ActionDispatch::Cookies",
......
......@@ -254,7 +254,7 @@ def self.call(env)
require 'rack/test'
extend Rack::Test::Methods
get "/"
get "/not/slash"
assert_equal 200, last_response.status
assert_equal "FooMetal", last_response.body
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册