From 455de6743f6f2c7b48d7eee129298e485b994f51 Mon Sep 17 00:00:00 2001 From: rain Date: Thu, 13 Jan 2011 12:43:21 +0800 Subject: [PATCH] You can control request's body in test code. --- actionpack/lib/action_controller/test_case.rb | 22 ++++++++++++++++--- .../test/controller/request/test_request_test.rb | 14 +++++++++++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb index 0f43527..0f4b3bc 100644 --- a/actionpack/lib/action_controller/test_case.rb +++ b/actionpack/lib/action_controller/test_case.rb @@ -127,7 +127,7 @@ module ActionController class TestRequest < ActionDispatch::TestRequest #:nodoc: def initialize(env = {}) super - + @set_body = false self.session = TestSession.new self.session_options = TestSession::DEFAULT_OPTIONS.merge(:id => ActiveSupport::SecureRandom.hex(16)) end @@ -166,9 +166,11 @@ module ActionController params.delete(k.to_sym) end data = params.to_query - - @env['CONTENT_LENGTH'] = data.length.to_s - @env['rack.input'] = StringIO.new(data) + + unless @set_body + @env['CONTENT_LENGTH'] = data.length.to_s + @env['rack.input'] = StringIO.new(data) + end end def recycle! @@ -180,6 +182,18 @@ module ActionController @fullpath = @ip = @remote_ip = nil @env['action_dispatch.request.query_parameters'] = {} end + + def body=(value) + @set_body = true + @env['CONTENT_LENGTH'] = value.length.to_s + @env["rack.input"] = StringIO.new(value) + self + end + + def set_header(key, value) + @env[key] = value + end + end class TestResponse < ActionDispatch::TestResponse diff --git a/actionpack/test/controller/request/test_request_test.rb b/actionpack/test/controller/request/test_request_test.rb index 0a39feb..9f58dcb 100644 --- a/actionpack/test/controller/request/test_request_test.rb +++ b/actionpack/test/controller/request/test_request_test.rb @@ -33,4 +33,16 @@ class ActionController::TestRequestTest < ActiveSupport::TestCase assert_not_equal(@request.session_options[:id], ActionController::TestRequest.new.session_options[:id]) end -end \ No newline at end of file + def test_set_body + @request.body="Test body" + router = mock() + router.stubs(:extra_keys).returns({}) + @request.assign_parameters(router, 'fake_controller', 'fake_action', {}) + assert_equal("Test body", @request.body.read()) + end + + def test_set_header + @request.set_header('TEST_HEADER', 'TEST VALUE') + assert_equal('TEST VALUE', @request.headers['TEST_HEADER']) + end +end -- 1.7.1