2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-09 13:35:59 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
|
|
|
|
class ParameterEncodingController < ActionController::Base
|
2016-12-21 14:21:01 -05:00
|
|
|
skip_parameter_encoding :test_bar
|
|
|
|
skip_parameter_encoding :test_all_values_encoding
|
2016-08-09 13:35:59 -04:00
|
|
|
|
|
|
|
def test_foo
|
|
|
|
render body: params[:foo].encoding
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_bar
|
|
|
|
render body: params[:bar].encoding
|
|
|
|
end
|
|
|
|
|
2016-12-21 14:21:01 -05:00
|
|
|
def test_all_values_encoding
|
2020-06-22 04:37:53 -04:00
|
|
|
render body: ::JSON.dump(params.except(:action, :controller).values.map(&:encoding).map(&:name))
|
2016-08-09 13:35:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ParameterEncodingTest < ActionController::TestCase
|
|
|
|
tests ParameterEncodingController
|
|
|
|
|
|
|
|
test "properly transcodes UTF8 parameters into declared encodings" do
|
2016-08-16 03:30:11 -04:00
|
|
|
post :test_foo, params: { "foo" => "foo", "bar" => "bar", "baz" => "baz" }
|
2016-08-09 13:35:59 -04:00
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert_equal "UTF-8", @response.body
|
|
|
|
end
|
|
|
|
|
2016-12-21 14:21:01 -05:00
|
|
|
test "properly encodes ASCII_8BIT parameters into binary" do
|
2016-08-16 03:30:11 -04:00
|
|
|
post :test_bar, params: { "foo" => "foo", "bar" => "bar", "baz" => "baz" }
|
2016-08-09 13:35:59 -04:00
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert_equal "ASCII-8BIT", @response.body
|
|
|
|
end
|
|
|
|
|
2016-12-21 14:21:01 -05:00
|
|
|
test "properly encodes all ASCII_8BIT parameters into binary" do
|
|
|
|
post :test_all_values_encoding, params: { "foo" => "foo", "bar" => "bar", "baz" => "baz" }
|
2016-08-09 13:35:59 -04:00
|
|
|
|
|
|
|
assert_response :success
|
2016-12-21 14:21:01 -05:00
|
|
|
assert_equal ["ASCII-8BIT"], JSON.parse(@response.body).uniq
|
2016-08-09 13:35:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "does not raise an error when passed a param declared as ASCII-8BIT that contains invalid bytes" do
|
2020-06-27 06:19:31 -04:00
|
|
|
get :test_bar, params: { "bar" => URI::DEFAULT_PARSER.escape("bar\xE2baz".b) }
|
2016-08-09 13:35:59 -04:00
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert_equal "ASCII-8BIT", @response.body
|
|
|
|
end
|
|
|
|
end
|