From ce99c87551c46ede3f7780282f9156d40a251a03 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 1 Jun 2006 00:01:48 +0000 Subject: Cope with missing content type and length headers. Parse parameters from multipart and urlencoded request bodies only. Accept multipart PUT parameters. Closes #5235. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4388 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/test/controller/cgi_test.rb | 9 ++++ actionpack/test/controller/raw_post_test.rb | 67 ++++++++++++++++++++++------- 2 files changed, 61 insertions(+), 15 deletions(-) (limited to 'actionpack/test/controller') diff --git a/actionpack/test/controller/cgi_test.rb b/actionpack/test/controller/cgi_test.rb index 06f721997d..0415a1384c 100755 --- a/actionpack/test/controller/cgi_test.rb +++ b/actionpack/test/controller/cgi_test.rb @@ -235,6 +235,7 @@ class CGITest < Test::Unit::TestCase end end + class MultipartCGITest < Test::Unit::TestCase FIXTURE_PATH = File.dirname(__FILE__) + '/../fixtures/multipart' @@ -315,6 +316,14 @@ class MultipartCGITest < Test::Unit::TestCase end end +# Ensures that PUT works with multipart as well as POST. +class PutMultipartCGITest < MultipartCGITest + def setup + super + ENV['REQUEST_METHOD'] = 'PUT' + end +end + class CGIRequestTest < Test::Unit::TestCase def setup diff --git a/actionpack/test/controller/raw_post_test.rb b/actionpack/test/controller/raw_post_test.rb index 98dc7f6ba7..b6895bc3cc 100644 --- a/actionpack/test/controller/raw_post_test.rb +++ b/actionpack/test/controller/raw_post_test.rb @@ -5,27 +5,64 @@ require File.dirname(__FILE__) + '/../../lib/action_controller/cgi_ext/raw_post_ class RawPostDataTest < Test::Unit::TestCase def setup + ENV.delete('RAW_POST_DATA') + @request_body = 'a=1' + end + + def test_post_with_urlencoded_body + ENV['REQUEST_METHOD'] = 'POST' + ENV['CONTENT_TYPE'] = 'application/x-www-form-urlencoded' + assert_equal ['1'], cgi_params['a'] + assert_has_raw_post_data + end + + def test_post_with_empty_content_type_treated_as_urlencoded ENV['REQUEST_METHOD'] = 'POST' ENV['CONTENT_TYPE'] = '' - ENV['CONTENT_LENGTH'] = '0' + assert_equal ['1'], cgi_params['a'] + assert_has_raw_post_data + end + + def test_post_with_unrecognized_content_type_reads_body_but_doesnt_parse_params + ENV['REQUEST_METHOD'] = 'POST' + ENV['CONTENT_TYPE'] = 'foo/bar' + assert cgi_params.empty? + assert_has_raw_post_data end - def test_raw_post_data - process_raw "action=create_customer&full_name=David%20Heinemeier%20Hansson&customerId=1" + 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 + 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 + 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 end private - def process_raw(query_string) - old_stdin = $stdin - begin - $stdin = StringIO.new(query_string.dup) - ENV['CONTENT_LENGTH'] = $stdin.size.to_s - CGI.new - assert_not_nil ENV['RAW_POST_DATA'] - assert ENV['RAW_POST_DATA'].frozen? - assert_equal query_string, ENV['RAW_POST_DATA'] - ensure - $stdin = old_stdin - end + 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 + end + + def assert_has_raw_post_data(expected_body = @request_body) + assert_not_nil ENV['RAW_POST_DATA'] + assert ENV['RAW_POST_DATA'].frozen? + assert_equal expected_body, ENV['RAW_POST_DATA'] end end -- cgit v1.2.3