diff options
author | Yehuda Katz <wycats@gmail.com> | 2009-01-13 14:24:10 -0800 |
---|---|---|
committer | Yehuda Katz <wycats@gmail.com> | 2009-01-13 14:24:14 -0800 |
commit | f65333a4cfcdc73b2f2e51fc4353370ab308135e (patch) | |
tree | 77a2ca3af3a54bc5412385230dd57b0641bba945 | |
parent | 0932b012fae2ff21214f610a0fd78a55ae4e2420 (diff) | |
parent | 1adc1496f9152c893e1f08abcb1e5e7272829899 (diff) | |
download | rails-f65333a4cfcdc73b2f2e51fc4353370ab308135e.tar.gz rails-f65333a4cfcdc73b2f2e51fc4353370ab308135e.tar.bz2 rails-f65333a4cfcdc73b2f2e51fc4353370ab308135e.zip |
Sync 'rails/rails/master'
5 files changed, 128 insertions, 8 deletions
diff --git a/actionpack/lib/action_controller.rb b/actionpack/lib/action_controller.rb index 8c022e5625..0d81b9c346 100644 --- a/actionpack/lib/action_controller.rb +++ b/actionpack/lib/action_controller.rb @@ -59,16 +59,14 @@ module ActionController autoload :MiddlewareStack, 'action_controller/middleware_stack' autoload :MimeResponds, 'action_controller/mime_responds' autoload :PolymorphicRoutes, 'action_controller/polymorphic_routes' - autoload :Request, 'action_controller/request' - autoload :RequestParser, 'action_controller/request_parser' - autoload :UrlEncodedPairParser, 'action_controller/url_encoded_pair_parser' - autoload :UploadedStringIO, 'action_controller/uploaded_file' - autoload :UploadedTempfile, 'action_controller/uploaded_file' autoload :RecordIdentifier, 'action_controller/record_identifier' - autoload :Response, 'action_controller/response' + autoload :Request, 'action_controller/request' autoload :RequestForgeryProtection, 'action_controller/request_forgery_protection' + autoload :RequestParser, 'action_controller/request_parser' autoload :Rescue, 'action_controller/rescue' autoload :Resources, 'action_controller/resources' + autoload :Response, 'action_controller/response' + autoload :RewindableInput, 'action_controller/rewindable_input' autoload :Routing, 'action_controller/routing' autoload :SessionManagement, 'action_controller/session_management' autoload :StatusCodes, 'action_controller/status_codes' @@ -76,6 +74,9 @@ module ActionController autoload :TestCase, 'action_controller/test_case' autoload :TestProcess, 'action_controller/test_process' autoload :Translation, 'action_controller/translation' + autoload :UploadedStringIO, 'action_controller/uploaded_file' + autoload :UploadedTempfile, 'action_controller/uploaded_file' + autoload :UrlEncodedPairParser, 'action_controller/url_encoded_pair_parser' autoload :UrlRewriter, 'action_controller/url_rewriter' autoload :UrlWriter, 'action_controller/url_rewriter' autoload :VerbPiggybacking, 'action_controller/verb_piggybacking' diff --git a/actionpack/lib/action_controller/middlewares.rb b/actionpack/lib/action_controller/middlewares.rb index 793739723f..914750bc0c 100644 --- a/actionpack/lib/action_controller/middlewares.rb +++ b/actionpack/lib/action_controller/middlewares.rb @@ -18,4 +18,5 @@ use "ActiveRecord::QueryCache", :if => lambda { defined?(ActiveRecord) } ) end +use ActionController::RewindableInput use ActionController::VerbPiggybacking diff --git a/actionpack/lib/action_controller/rewindable_input.rb b/actionpack/lib/action_controller/rewindable_input.rb new file mode 100644 index 0000000000..296d8aed22 --- /dev/null +++ b/actionpack/lib/action_controller/rewindable_input.rb @@ -0,0 +1,40 @@ +module ActionController + class RewindableInput + class RewindableIO < ActiveSupport::BasicObject + def initialize(io) + @io = io + end + + def read(*args) + read_original_io + @io.read(*args) + end + + def rewind + read_original_io + @io.rewind + end + + def method_missing(method, *args, &block) + @io.send(method, *args, &block) + end + + private + def read_original_io + unless @str + @str = @io.read + @io = StringIO.new(@str) + end + end + end + + def initialize(app) + @app = app + end + + def call(env) + env['rack.input'] = RewindableIO.new(env['rack.input']) + @app.call(env) + end + end +end diff --git a/actionpack/test/controller/request/multipart_params_parsing_test.rb b/actionpack/test/controller/request/multipart_params_parsing_test.rb index ce28ff46fe..d976ab8512 100644 --- a/actionpack/test/controller/request/multipart_params_parsing_test.rb +++ b/actionpack/test/controller/request/multipart_params_parsing_test.rb @@ -123,14 +123,14 @@ class MultipartParamsParsingTest < ActionController::IntegrationTest InputWrapper = Rack::Lint::InputWrapper test "parses unwindable stream" do - InputWrapper.any_instance.expects(:rewind).raises(Errno::ESPIPE) + InputWrapper.any_instance.stubs(:rewind).raises(Errno::ESPIPE) params = parse_multipart('large_text_file') assert_equal %w(file foo), params.keys.sort assert_equal 'bar', params['foo'] end test "uploads and reads file with unwindable input" do - InputWrapper.any_instance.expects(:rewind).raises(Errno::ESPIPE) + InputWrapper.any_instance.stubs(:rewind).raises(Errno::ESPIPE) with_test_routing do post '/read', :uploaded_data => fixture_file_upload(FIXTURE_PATH + "/hello.txt", "text/plain") @@ -138,6 +138,26 @@ class MultipartParamsParsingTest < ActionController::IntegrationTest end end + test "passes through rack middleware and uploads file" do + with_muck_middleware do + with_test_routing do + post '/read', :uploaded_data => fixture_file_upload(FIXTURE_PATH + "/hello.txt", "text/plain") + assert_equal "File: Hello", response.body + end + end + end + + test "passes through rack middleware and uploads file with unwindable input" do + InputWrapper.any_instance.stubs(:rewind).raises(Errno::ESPIPE) + + with_muck_middleware do + with_test_routing do + post '/read', :uploaded_data => fixture_file_upload(FIXTURE_PATH + "/hello.txt", "text/plain") + assert_equal "File: Hello", response.body + end + end + end + private def fixture(name) File.open(File.join(FIXTURE_PATH, name), 'rb') do |file| @@ -164,4 +184,25 @@ class MultipartParamsParsingTest < ActionController::IntegrationTest yield end end + + class MuckMiddleware + def initialize(app) + @app = app + end + + def call(env) + req = Rack::Request.new(env) + req.params # Parse params + @app.call(env) + end + end + + def with_muck_middleware + original_middleware = ActionController::Dispatcher.middleware + middleware = original_middleware.dup + middleware.use MuckMiddleware + ActionController::Dispatcher.middleware = middleware + yield + ActionController::Dispatcher.middleware = original_middleware + end end diff --git a/actionpack/test/controller/request/url_encoded_params_parsing_test.rb b/actionpack/test/controller/request/url_encoded_params_parsing_test.rb index 26a4538ca6..b162796e5b 100644 --- a/actionpack/test/controller/request/url_encoded_params_parsing_test.rb +++ b/actionpack/test/controller/request/url_encoded_params_parsing_test.rb @@ -150,8 +150,45 @@ class UrlEncodedParamsParsingTest < ActionController::IntegrationTest assert_parses expected, query end + test "passes through rack middleware and parses params" do + with_muck_middleware do + assert_parses({ "a" => { "b" => "c" } }, "a[b]=c") + end + end + + # The lint wrapper is used in integration tests + # instead of a normal StringIO class + InputWrapper = Rack::Lint::InputWrapper + + test "passes through rack middleware and parses params with unwindable input" do + InputWrapper.any_instance.stubs(:rewind).raises(Errno::ESPIPE) + with_muck_middleware do + assert_parses({ "a" => { "b" => "c" } }, "a[b]=c") + end + end private + class MuckMiddleware + def initialize(app) + @app = app + end + + def call(env) + req = Rack::Request.new(env) + req.params # Parse params + @app.call(env) + end + end + + def with_muck_middleware + original_middleware = ActionController::Dispatcher.middleware + middleware = original_middleware.dup + middleware.use MuckMiddleware + ActionController::Dispatcher.middleware = middleware + yield + ActionController::Dispatcher.middleware = original_middleware + end + def with_test_routing with_routing do |set| set.draw do |map| |