aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/raw_post_test.rb
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/test/controller/raw_post_test.rb')
-rw-r--r--actionpack/test/controller/raw_post_test.rb53
1 files changed, 30 insertions, 23 deletions
diff --git a/actionpack/test/controller/raw_post_test.rb b/actionpack/test/controller/raw_post_test.rb
index 1f8a93d149..5fd2c84ff0 100644
--- a/actionpack/test/controller/raw_post_test.rb
+++ b/actionpack/test/controller/raw_post_test.rb
@@ -1,6 +1,4 @@
require "#{File.dirname(__FILE__)}/../abstract_unit"
-require 'stringio'
-require 'action_controller/cgi_ext/query_extension'
class RawPostDataTest < Test::Unit::TestCase
def setup
@@ -11,57 +9,66 @@ class RawPostDataTest < Test::Unit::TestCase
def test_post_with_urlencoded_body
ENV['REQUEST_METHOD'] = 'POST'
ENV['CONTENT_TYPE'] = ' apPlication/x-Www-form-urlEncoded; charset=utf-8'
- assert_equal ['1'], cgi_params['a']
- assert_has_raw_post_data
+ assert_equal ['1'], cgi.params['a']
+ assert_raw_post_data
end
def test_post_with_empty_content_type_treated_as_urlencoded
ENV['REQUEST_METHOD'] = 'POST'
ENV['CONTENT_TYPE'] = ''
- assert_equal ['1'], cgi_params['a']
- assert_has_raw_post_data
+ assert_equal ['1'], cgi.params['a']
+ assert_raw_post_data
end
- def test_post_with_unrecognized_content_type_reads_body_but_doesnt_parse_params
+ def test_post_with_unrecognized_content_type_ignores_body
ENV['REQUEST_METHOD'] = 'POST'
ENV['CONTENT_TYPE'] = 'foo/bar'
- assert cgi_params.empty?
- assert_has_raw_post_data
+ assert cgi.params.empty?
+ assert_no_raw_post_data
end
def test_put_with_urlencoded_body
ENV['REQUEST_METHOD'] = 'PUT'
ENV['CONTENT_TYPE'] = 'application/x-www-form-urlencoded'
- assert_equal ['1'], cgi_params['a']
- assert_has_raw_post_data
+ assert_equal ['1'], cgi.params['a']
+ assert_raw_post_data
end
def test_put_with_empty_content_type_ignores_body
ENV['REQUEST_METHOD'] = 'PUT'
ENV['CONTENT_TYPE'] = ''
- assert cgi_params.empty?
- assert_has_raw_post_data
+ assert cgi.params.empty?
+ assert_no_raw_post_data
end
def test_put_with_unrecognized_content_type_ignores_body
ENV['REQUEST_METHOD'] = 'PUT'
ENV['CONTENT_TYPE'] = 'foo/bar'
- assert cgi_params.empty?
- assert_has_raw_post_data
+ assert cgi.params.empty?
+ assert_no_raw_post_data
end
private
- def cgi_params
- old_stdin, $stdin = $stdin, StringIO.new(@request_body.dup)
- ENV['CONTENT_LENGTH'] = $stdin.size.to_s
- CGI.new.params
- ensure
- $stdin = old_stdin
+ def cgi
+ unless defined? @cgi
+ ENV['CONTENT_LENGTH'] = @request_body.size.to_s
+ @cgi = CGI.new('query', StringIO.new(@request_body.dup))
+ end
+
+ @cgi
end
- def assert_has_raw_post_data(expected_body = @request_body)
+ def assert_raw_post_data
assert_not_nil ENV['RAW_POST_DATA']
assert ENV['RAW_POST_DATA'].frozen?
- assert_equal expected_body, ENV['RAW_POST_DATA']
+ assert_equal @request_body, ENV['RAW_POST_DATA']
+
+ assert_equal '', cgi.stdinput.read
+ end
+
+ def assert_no_raw_post_data
+ assert_nil ENV['RAW_POST_DATA']
+
+ assert_equal @request_body, cgi.stdinput.read
end
end