aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/CHANGELOG.md2
-rw-r--r--actionpack/lib/action_dispatch/http/mime_type.rb26
-rw-r--r--actionpack/lib/action_dispatch/routing/mapper.rb17
-rw-r--r--actionpack/lib/action_dispatch/testing/assertions/response.rb15
-rw-r--r--actionpack/test/assertions/response_assertions_test.rb17
-rw-r--r--actionpack/test/dispatch/mapper_test.rb14
-rw-r--r--actionpack/test/dispatch/request_test.rb56
7 files changed, 94 insertions, 53 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md
index a62f655dc2..cab7d85ee7 100644
--- a/actionpack/CHANGELOG.md
+++ b/actionpack/CHANGELOG.md
@@ -2,6 +2,8 @@
is empty anyway. (It used to do that when called like `url_for(controller:
'x', action: 'y', q: {})`.)
+ *Paul Grayson*
+
* Catch invalid UTF-8 querystring values and respond with BadRequest
Check querystring params for invalid UTF-8 characters, and raise an
diff --git a/actionpack/lib/action_dispatch/http/mime_type.rb b/actionpack/lib/action_dispatch/http/mime_type.rb
index b64f660ec5..b8d395854c 100644
--- a/actionpack/lib/action_dispatch/http/mime_type.rb
+++ b/actionpack/lib/action_dispatch/http/mime_type.rb
@@ -47,15 +47,10 @@ module Mime
def const_missing(sym)
ext = sym.downcase
if Mime[ext]
- ActiveSupport::Deprecation.warn <<-eow
-Accessing mime types via constants is deprecated. Please change:
-
- `Mime::#{sym}`
-
-to:
-
- `Mime[:#{ext}]`
- eow
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
+ Accessing mime types via constants is deprecated.
+ Please change `Mime::#{sym}` to `Mime[:#{ext}]`.
+ MSG
Mime[ext]
else
super
@@ -65,15 +60,10 @@ to:
def const_defined?(sym, inherit = true)
ext = sym.downcase
if Mime[ext]
- ActiveSupport::Deprecation.warn <<-eow
-Accessing mime types via constants is deprecated. Please change:
-
- `Mime.const_defined?(#{sym})`
-
-to:
-
- `Mime[:#{ext}]`
- eow
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
+ Accessing mime types via constants is deprecated.
+ Please change `Mime.const_defined?(#{sym})` to `Mime[:#{ext}]`.
+ MSG
true
else
super
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb
index 7c0404ca62..18cd205bad 100644
--- a/actionpack/lib/action_dispatch/routing/mapper.rb
+++ b/actionpack/lib/action_dispatch/routing/mapper.rb
@@ -11,7 +11,7 @@ module ActionDispatch
class Mapper
URL_OPTIONS = [:protocol, :subdomain, :domain, :host, :port]
- class Constraints < Endpoint #:nodoc:
+ class Constraints < Routing::Endpoint #:nodoc:
attr_reader :app, :constraints
SERVE = ->(app, req) { app.serve req }
@@ -600,17 +600,20 @@ module ActionDispatch
def mount(app, options = nil)
if options
path = options.delete(:at)
- else
- unless Hash === app
- raise ArgumentError, "must be called with mount point"
- end
-
+ elsif Hash === app
options = app
app, path = options.find { |k, _| k.respond_to?(:call) }
options.delete(app) if app
end
- raise "A rack application must be specified" unless path
+ raise ArgumentError, "A rack application must be specified" unless app.respond_to?(:call)
+ raise ArgumentError, <<-MSG.strip_heredoc unless path
+ Must be called with mount point
+
+ mount SomeRackApp, at: "some_route"
+ or
+ mount(SomeRackApp => "some_route")
+ MSG
rails_app = rails_app? app
options[:as] ||= app_name(app, rails_app)
diff --git a/actionpack/lib/action_dispatch/testing/assertions/response.rb b/actionpack/lib/action_dispatch/testing/assertions/response.rb
index eab20b075d..5af052afb4 100644
--- a/actionpack/lib/action_dispatch/testing/assertions/response.rb
+++ b/actionpack/lib/action_dispatch/testing/assertions/response.rb
@@ -27,6 +27,8 @@ module ActionDispatch
# # Asserts that the response code was status code 401 (unauthorized)
# assert_response 401
def assert_response(type, message = nil)
+ message ||= generate_response_message(type)
+
if Symbol === type
if [:success, :missing, :redirect, :error].include?(type)
assert_predicate @response, RESPONSE_PREDICATES[type], message
@@ -82,6 +84,19 @@ module ActionDispatch
handle._compute_redirect_to_location(@request, fragment)
end
end
+
+ def generate_response_message(type)
+ message = "Expected response to be a <#{type}>, but was"
+
+ if @response.redirection?
+ redirect_is = normalize_argument_to_redirection(@response.location)
+ message << " a redirect to <#{redirect_is}>"
+ else
+ message << " <#{@response.response_code}>"
+ end
+
+ message
+ end
end
end
end
diff --git a/actionpack/test/assertions/response_assertions_test.rb b/actionpack/test/assertions/response_assertions_test.rb
index 82c747680d..6c7036aa1a 100644
--- a/actionpack/test/assertions/response_assertions_test.rb
+++ b/actionpack/test/assertions/response_assertions_test.rb
@@ -6,7 +6,12 @@ module ActionDispatch
class ResponseAssertionsTest < ActiveSupport::TestCase
include ResponseAssertions
- FakeResponse = Struct.new(:response_code) do
+ FakeResponse = Struct.new(:response_code, :location) do
+ def initialize(*)
+ super
+ self.location ||= "http://test.example.com/posts"
+ end
+
[:successful, :not_found, :redirection, :server_error].each do |sym|
define_method("#{sym}?") do
sym == response_code
@@ -58,6 +63,16 @@ module ActionDispatch
assert_response :succezz
}
end
+
+ def test_message_when_response_is_redirect_but_asserted_for_status_other_than_redirect
+ @response = FakeResponse.new :redirection, "http://test.host/posts/redirect/1"
+ error = assert_raises(Minitest::Assertion) do
+ assert_response :success
+ end
+
+ expected = "Expected response to be a <success>, but was a redirect to <http://test.host/posts/redirect/1>."
+ assert_match expected, error.message
+ end
end
end
end
diff --git a/actionpack/test/dispatch/mapper_test.rb b/actionpack/test/dispatch/mapper_test.rb
index e783df855e..df27e41997 100644
--- a/actionpack/test/dispatch/mapper_test.rb
+++ b/actionpack/test/dispatch/mapper_test.rb
@@ -158,7 +158,7 @@ module ActionDispatch
assert_equal '/*path.:format', fakeset.asts.first.to_s
end
- def test_raising_helpful_error_on_invalid_arguments
+ def test_raising_error_when_path_is_not_passed
fakeset = FakeSet.new
mapper = Mapper.new fakeset
app = lambda { |env| [200, {}, [""]] }
@@ -166,6 +166,18 @@ module ActionDispatch
mapper.mount app
end
end
+
+ def test_raising_error_when_rack_app_is_not_passed
+ fakeset = FakeSet.new
+ mapper = Mapper.new fakeset
+ assert_raises ArgumentError do
+ mapper.mount 10, as: "exciting"
+ end
+
+ assert_raises ArgumentError do
+ mapper.mount as: "exciting"
+ end
+ end
end
end
end
diff --git a/actionpack/test/dispatch/request_test.rb b/actionpack/test/dispatch/request_test.rb
index 08c4554721..7dd9d05e62 100644
--- a/actionpack/test/dispatch/request_test.rb
+++ b/actionpack/test/dispatch/request_test.rb
@@ -4,9 +4,13 @@ class BaseRequestTest < ActiveSupport::TestCase
def setup
@env = {
:ip_spoofing_check => true,
- :tld_length => 1,
"rack.input" => "foo"
}
+ @original_tld_length = ActionDispatch::Http::URL.tld_length
+ end
+
+ def teardown
+ ActionDispatch::Http::URL.tld_length = @original_tld_length
end
def url_for(options = {})
@@ -19,9 +23,9 @@ class BaseRequestTest < ActiveSupport::TestCase
ip_spoofing_check = env.key?(:ip_spoofing_check) ? env.delete(:ip_spoofing_check) : true
@trusted_proxies ||= nil
ip_app = ActionDispatch::RemoteIp.new(Proc.new { }, ip_spoofing_check, @trusted_proxies)
- tld_length = env.key?(:tld_length) ? env.delete(:tld_length) : 1
+ ActionDispatch::Http::URL.tld_length = env.delete(:tld_length) if env.key?(:tld_length)
+
ip_app.call(env)
- ActionDispatch::Http::URL.tld_length = tld_length
env = @env.merge(env)
ActionDispatch::Request.new(env)
@@ -254,15 +258,6 @@ end
class RequestDomain < BaseRequestTest
test "domains" do
- request = stub_request 'HTTP_HOST' => 'www.rubyonrails.org'
- assert_equal "rubyonrails.org", request.domain
-
- request = stub_request 'HTTP_HOST' => "www.rubyonrails.co.uk"
- assert_equal "rubyonrails.co.uk", request.domain(2)
-
- request = stub_request 'HTTP_HOST' => "www.rubyonrails.co.uk", :tld_length => 2
- assert_equal "rubyonrails.co.uk", request.domain
-
request = stub_request 'HTTP_HOST' => "192.168.1.200"
assert_nil request.domain
@@ -271,25 +266,18 @@ class RequestDomain < BaseRequestTest
request = stub_request 'HTTP_HOST' => "192.168.1.200.com"
assert_equal "200.com", request.domain
- end
- test "subdomains" do
- request = stub_request 'HTTP_HOST' => "www.rubyonrails.org"
- assert_equal %w( www ), request.subdomains
- assert_equal "www", request.subdomain
+ request = stub_request 'HTTP_HOST' => 'www.rubyonrails.org'
+ assert_equal "rubyonrails.org", request.domain
request = stub_request 'HTTP_HOST' => "www.rubyonrails.co.uk"
- assert_equal %w( www ), request.subdomains(2)
- assert_equal "www", request.subdomain(2)
-
- request = stub_request 'HTTP_HOST' => "dev.www.rubyonrails.co.uk"
- assert_equal %w( dev www ), request.subdomains(2)
- assert_equal "dev.www", request.subdomain(2)
+ assert_equal "rubyonrails.co.uk", request.domain(2)
- request = stub_request 'HTTP_HOST' => "dev.www.rubyonrails.co.uk", :tld_length => 2
- assert_equal %w( dev www ), request.subdomains
- assert_equal "dev.www", request.subdomain
+ request = stub_request 'HTTP_HOST' => "www.rubyonrails.co.uk", :tld_length => 2
+ assert_equal "rubyonrails.co.uk", request.domain
+ end
+ test "subdomains" do
request = stub_request 'HTTP_HOST' => "foobar.foobar.com"
assert_equal %w( foobar ), request.subdomains
assert_equal "foobar", request.subdomain
@@ -309,6 +297,22 @@ class RequestDomain < BaseRequestTest
request = stub_request 'HTTP_HOST' => nil
assert_equal [], request.subdomains
assert_equal "", request.subdomain
+
+ request = stub_request 'HTTP_HOST' => "www.rubyonrails.org"
+ assert_equal %w( www ), request.subdomains
+ assert_equal "www", request.subdomain
+
+ request = stub_request 'HTTP_HOST' => "www.rubyonrails.co.uk"
+ assert_equal %w( www ), request.subdomains(2)
+ assert_equal "www", request.subdomain(2)
+
+ request = stub_request 'HTTP_HOST' => "dev.www.rubyonrails.co.uk"
+ assert_equal %w( dev www ), request.subdomains(2)
+ assert_equal "dev.www", request.subdomain(2)
+
+ request = stub_request 'HTTP_HOST' => "dev.www.rubyonrails.co.uk", :tld_length => 2
+ assert_equal %w( dev www ), request.subdomains
+ assert_equal "dev.www", request.subdomain
end
end