|
| 1 | +require "abstract_unit" |
| 2 | + |
| 3 | +class ParameterEncodingController < ActionController::Base |
| 4 | + parameter_encoding :test_bar, :bar, Encoding::ASCII_8BIT |
| 5 | + parameter_encoding :test_baz, :baz, Encoding::ISO_8859_1 |
| 6 | + parameter_encoding :test_baz_to_ascii, :baz, Encoding::ASCII_8BIT |
| 7 | + |
| 8 | + def test_foo |
| 9 | + render body: params[:foo].encoding |
| 10 | + end |
| 11 | + |
| 12 | + def test_bar |
| 13 | + render body: params[:bar].encoding |
| 14 | + end |
| 15 | + |
| 16 | + def test_baz |
| 17 | + render body: params[:baz].encoding |
| 18 | + end |
| 19 | + |
| 20 | + def test_no_change_to_baz |
| 21 | + render body: params[:baz].encoding |
| 22 | + end |
| 23 | + |
| 24 | + def test_baz_to_ascii |
| 25 | + render body: params[:baz].encoding |
| 26 | + end |
| 27 | +end |
| 28 | + |
| 29 | +class ParameterEncodingTest < ActionController::TestCase |
| 30 | + tests ParameterEncodingController |
| 31 | + |
| 32 | + test "properly transcodes UTF8 parameters into declared encodings" do |
| 33 | + post :test_foo, params: {"foo" => "foo", "bar" => "bar", "baz" => "baz"} |
| 34 | + |
| 35 | + assert_response :success |
| 36 | + assert_equal "UTF-8", @response.body |
| 37 | + end |
| 38 | + |
| 39 | + test "properly transcodes ASCII_8BIT parameters into declared encodings" do |
| 40 | + post :test_bar, params: {"foo" => "foo", "bar" => "bar", "baz" => "baz"} |
| 41 | + |
| 42 | + assert_response :success |
| 43 | + assert_equal "ASCII-8BIT", @response.body |
| 44 | + end |
| 45 | + |
| 46 | + test "properly transcodes ISO_8859_1 parameters into declared encodings" do |
| 47 | + post :test_baz, params: {"foo" => "foo", "bar" => "bar", "baz" => "baz"} |
| 48 | + |
| 49 | + assert_response :success |
| 50 | + assert_equal "ISO-8859-1", @response.body |
| 51 | + end |
| 52 | + |
| 53 | + test "does not transcode parameters when not specified" do |
| 54 | + post :test_no_change_to_baz, params: {"foo" => "foo", "bar" => "bar", "baz" => "baz"} |
| 55 | + |
| 56 | + assert_response :success |
| 57 | + assert_equal "UTF-8", @response.body |
| 58 | + end |
| 59 | + |
| 60 | + test "respects different encoding declarations for a param per action" do |
| 61 | + post :test_baz_to_ascii, params: {"foo" => "foo", "bar" => "bar", "baz" => "baz"} |
| 62 | + |
| 63 | + assert_response :success |
| 64 | + assert_equal "ASCII-8BIT", @response.body |
| 65 | + end |
| 66 | + |
| 67 | + test "does not raise an error when passed a param declared as ASCII-8BIT that contains invalid bytes" do |
| 68 | + get :test_bar, params: { "bar" => URI.parser.escape("bar\xE2baz".b) } |
| 69 | + |
| 70 | + assert_response :success |
| 71 | + assert_equal "ASCII-8BIT", @response.body |
| 72 | + end |
| 73 | +end |
0 commit comments