aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test
diff options
context:
space:
mode:
authorKerri Miller <kerrizor@github.com>2016-08-09 10:35:59 -0700
committerKerri Miller <kerrizor@github.com>2016-08-09 15:43:01 -0700
commit496d744fa31665de810b404de968ba86ed87c319 (patch)
tree5845602181062927cd72709facbb7d7eb92f6b42 /actionpack/test
parent46f511685c91486295a418547bb08c2aa5e49cfc (diff)
downloadrails-496d744fa31665de810b404de968ba86ed87c319.tar.gz
rails-496d744fa31665de810b404de968ba86ed87c319.tar.bz2
rails-496d744fa31665de810b404de968ba86ed87c319.zip
Allow specifying encoding of parameters by action
At GitHub we need to handle parameter encodings that are not UTF-8. This patch allows us to specify encodings per parameter per action.
Diffstat (limited to 'actionpack/test')
-rw-r--r--actionpack/test/abstract_unit.rb18
-rw-r--r--actionpack/test/controller/parameter_encoding_test.rb73
2 files changed, 78 insertions, 13 deletions
diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb
index 0c77de3c16..7bff21b5a2 100644
--- a/actionpack/test/abstract_unit.rb
+++ b/actionpack/test/abstract_unit.rb
@@ -122,27 +122,19 @@ class ActionDispatch::IntegrationTest < ActiveSupport::TestCase
# Stub Rails dispatcher so it does not get controller references and
# simply return the controller#action as Rack::Body.
class NullController < ::ActionController::Metal
- def initialize(controller_name)
- @controller = controller_name
- end
-
- def make_response!(request)
- self.class.make_response! request
- end
-
- def dispatch(action, req, res)
- [200, {"Content-Type" => "text/html"}, ["#{@controller}##{action}"]]
+ def self.dispatch(action, req, res)
+ [200, {"Content-Type" => "text/html"}, ["#{req.params[:controller]}##{action}"]]
end
end
- class NullControllerRequest < DelegateClass(ActionDispatch::Request)
+ class NullControllerRequest < ActionDispatch::Request
def controller_class
- NullController.new params[:controller]
+ NullController
end
end
def make_request(env)
- NullControllerRequest.new super
+ NullControllerRequest.new env
end
end
diff --git a/actionpack/test/controller/parameter_encoding_test.rb b/actionpack/test/controller/parameter_encoding_test.rb
new file mode 100644
index 0000000000..69a72c000b
--- /dev/null
+++ b/actionpack/test/controller/parameter_encoding_test.rb
@@ -0,0 +1,73 @@
+require "abstract_unit"
+
+class ParameterEncodingController < ActionController::Base
+ parameter_encoding :test_bar, :bar, Encoding::ASCII_8BIT
+ parameter_encoding :test_baz, :baz, Encoding::ISO_8859_1
+ parameter_encoding :test_baz_to_ascii, :baz, Encoding::ASCII_8BIT
+
+ def test_foo
+ render body: params[:foo].encoding
+ end
+
+ def test_bar
+ render body: params[:bar].encoding
+ end
+
+ def test_baz
+ render body: params[:baz].encoding
+ end
+
+ def test_no_change_to_baz
+ render body: params[:baz].encoding
+ end
+
+ def test_baz_to_ascii
+ render body: params[:baz].encoding
+ end
+end
+
+class ParameterEncodingTest < ActionController::TestCase
+ tests ParameterEncodingController
+
+ test "properly transcodes UTF8 parameters into declared encodings" do
+ post :test_foo, params: {"foo" => "foo", "bar" => "bar", "baz" => "baz"}
+
+ assert_response :success
+ assert_equal "UTF-8", @response.body
+ end
+
+ test "properly transcodes ASCII_8BIT parameters into declared encodings" do
+ post :test_bar, params: {"foo" => "foo", "bar" => "bar", "baz" => "baz"}
+
+ assert_response :success
+ assert_equal "ASCII-8BIT", @response.body
+ end
+
+ test "properly transcodes ISO_8859_1 parameters into declared encodings" do
+ post :test_baz, params: {"foo" => "foo", "bar" => "bar", "baz" => "baz"}
+
+ assert_response :success
+ assert_equal "ISO-8859-1", @response.body
+ end
+
+ test "does not transcode parameters when not specified" do
+ post :test_no_change_to_baz, params: {"foo" => "foo", "bar" => "bar", "baz" => "baz"}
+
+ assert_response :success
+ assert_equal "UTF-8", @response.body
+ end
+
+ test "respects different encoding declarations for a param per action" do
+ post :test_baz_to_ascii, params: {"foo" => "foo", "bar" => "bar", "baz" => "baz"}
+
+ assert_response :success
+ assert_equal "ASCII-8BIT", @response.body
+ end
+
+ test "does not raise an error when passed a param declared as ASCII-8BIT that contains invalid bytes" do
+ get :test_bar, params: { "bar" => URI.parser.escape("bar\xE2baz".b) }
+
+ assert_response :success
+ assert_equal "ASCII-8BIT", @response.body
+ end
+end