From 32d03af341eba4d0b263156f0241016b857c4d84 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 15 May 2007 21:36:21 +0000 Subject: Introduce the request.body stream. Lazy-read to parse parameters rather than always setting RAW_POST_DATA. Reduces the memory footprint of large binary PUT requests. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6740 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/test/abstract_unit.rb | 2 + actionpack/test/controller/raw_post_test.rb | 53 +++++++++++++++------------ actionpack/test/controller/test_test.rb | 15 +++++++- actionpack/test/controller/webservice_test.rb | 10 ++--- 4 files changed, 48 insertions(+), 32 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index e4b8b5b04a..3c576f3d20 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -3,8 +3,10 @@ $:.unshift(File.dirname(__FILE__) + '/../../activesupport/lib/active_support') $:.unshift(File.dirname(__FILE__) + '/fixtures/helpers') require 'yaml' +require 'stringio' require 'test/unit' require 'action_controller' +require 'action_controller/cgi_ext' require 'action_controller/test_process' # Show backtraces for deprecated behavior for quicker cleanup. 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 diff --git a/actionpack/test/controller/test_test.rb b/actionpack/test/controller/test_test.rb index ec30f0ac8b..d786a05feb 100644 --- a/actionpack/test/controller/test_test.rb +++ b/actionpack/test/controller/test_test.rb @@ -13,6 +13,10 @@ class TestTest < Test::Unit::TestCase render :text => request.raw_post end + def render_body + render :text => request.body.read + end + def test_params render :text => params.inspect end @@ -93,8 +97,15 @@ HTML params = {:page => {:name => 'page name'}, 'some key' => 123} get :render_raw_post, params.dup - raw_post = params.map {|k,v| [CGI::escape(k.to_s), CGI::escape(v.to_s)].join('=')}.sort.join('&') - assert_equal raw_post, @response.body + assert_equal params.to_query, @response.body + end + + def test_body_stream + params = { :page => { :name => 'page name' }, 'some key' => 123 } + + get :render_body, params.dup + + assert_equal params.to_query, @response.body end def test_process_without_flash diff --git a/actionpack/test/controller/webservice_test.rb b/actionpack/test/controller/webservice_test.rb index 4558d401b0..bcdb3d5eb7 100644 --- a/actionpack/test/controller/webservice_test.rb +++ b/actionpack/test/controller/webservice_test.rb @@ -1,20 +1,16 @@ require File.dirname(__FILE__) + '/../abstract_unit' -require 'stringio' class WebServiceTest < Test::Unit::TestCase - class MockCGI < CGI #:nodoc: - attr_accessor :stdinput, :stdoutput, :env_table + attr_accessor :stdoutput, :env_table - def initialize(env, data = '') + def initialize(env, data = '') self.env_table = env - self.stdinput = StringIO.new(data) self.stdoutput = StringIO.new - super() + super(nil, StringIO.new(data)) end end - class TestController < ActionController::Base session :off -- cgit v1.2.3