2005-01-20 09:55:13 -05:00
|
|
|
require 'test/unit'
|
|
|
|
require 'cgi'
|
|
|
|
require 'stringio'
|
|
|
|
require File.dirname(__FILE__) + '/../../lib/action_controller/cgi_ext/raw_post_data_fix'
|
|
|
|
|
|
|
|
class RawPostDataTest < Test::Unit::TestCase
|
|
|
|
def setup
|
2006-05-31 20:01:48 -04:00
|
|
|
ENV.delete('RAW_POST_DATA')
|
|
|
|
@request_body = 'a=1'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_post_with_urlencoded_body
|
|
|
|
ENV['REQUEST_METHOD'] = 'POST'
|
2006-06-30 01:41:03 -04:00
|
|
|
ENV['CONTENT_TYPE'] = ' apPlication/x-Www-form-urlEncoded; charset=utf-8'
|
2006-05-31 20:01:48 -04:00
|
|
|
assert_equal ['1'], cgi_params['a']
|
|
|
|
assert_has_raw_post_data
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_post_with_empty_content_type_treated_as_urlencoded
|
2005-01-20 09:55:13 -05:00
|
|
|
ENV['REQUEST_METHOD'] = 'POST'
|
|
|
|
ENV['CONTENT_TYPE'] = ''
|
2006-05-31 20:01:48 -04:00
|
|
|
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
|
2005-01-20 09:55:13 -05:00
|
|
|
end
|
|
|
|
|
2006-05-31 20:01:48 -04:00
|
|
|
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
|
2005-01-20 09:55:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2006-05-31 20:01:48 -04:00
|
|
|
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']
|
2005-01-20 09:55:13 -05:00
|
|
|
end
|
|
|
|
end
|