2009-01-09 17:05:27 -05:00
|
|
|
require 'abstract_unit'
|
|
|
|
|
2010-09-24 20:15:52 -04:00
|
|
|
class XmlParamsParsingTest < ActionDispatch::IntegrationTest
|
2009-01-09 17:05:27 -05:00
|
|
|
class TestController < ActionController::Base
|
|
|
|
class << self
|
|
|
|
attr_accessor :last_request_parameters
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse
|
|
|
|
self.class.last_request_parameters = request.request_parameters
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
TestController.last_request_parameters = nil
|
|
|
|
end
|
|
|
|
|
2010-05-06 07:40:48 -04:00
|
|
|
test "parses a strict rack.input" do
|
|
|
|
class Linted
|
2010-09-28 15:08:00 -04:00
|
|
|
undef call if method_defined?(:call)
|
2010-05-06 07:40:48 -04:00
|
|
|
def call(env)
|
|
|
|
bar = env['action_dispatch.request.request_parameters']['foo']
|
|
|
|
result = "<ok>#{bar}</ok>"
|
2010-05-15 14:25:56 -04:00
|
|
|
[200, {"Content-Type" => "application/xml", "Content-Length" => result.length.to_s}, [result]]
|
2010-05-06 07:40:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
req = Rack::MockRequest.new(ActionDispatch::ParamsParser.new(Linted.new))
|
|
|
|
resp = req.post('/', "CONTENT_TYPE" => "application/xml", :input => "<foo>bar</foo>", :lint => true)
|
|
|
|
assert_equal "<ok>bar</ok>", resp.body
|
|
|
|
end
|
|
|
|
|
2009-01-09 17:05:27 -05:00
|
|
|
test "parses hash params" do
|
|
|
|
with_test_routing do
|
|
|
|
xml = "<person><name>David</name></person>"
|
|
|
|
post "/parse", xml, default_headers
|
|
|
|
assert_response :ok
|
|
|
|
assert_equal({"person" => {"name" => "David"}}, TestController.last_request_parameters)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "parses single file" do
|
|
|
|
with_test_routing do
|
2012-01-01 12:57:55 -05:00
|
|
|
xml = "<person><name>David</name><avatar type='file' name='me.jpg' content_type='image/jpg'>#{::Base64.encode64('ABC')}</avatar></person>"
|
2009-01-09 17:05:27 -05:00
|
|
|
post "/parse", xml, default_headers
|
|
|
|
assert_response :ok
|
|
|
|
|
|
|
|
person = TestController.last_request_parameters
|
|
|
|
assert_equal "image/jpg", person['person']['avatar'].content_type
|
|
|
|
assert_equal "me.jpg", person['person']['avatar'].original_filename
|
|
|
|
assert_equal "ABC", person['person']['avatar'].read
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-11 00:56:46 -04:00
|
|
|
test "logs error if parsing unsuccessful" do
|
|
|
|
with_test_routing do
|
2011-11-24 14:37:48 -05:00
|
|
|
output = StringIO.new
|
2012-01-01 12:57:55 -05:00
|
|
|
xml = "<person><name>David</name><avatar type='file' name='me.jpg' content_type='image/jpg'>#{::Base64.encode64('ABC')}</avatar></pineapple>"
|
2012-01-04 13:52:24 -05:00
|
|
|
post "/parse", xml, default_headers.merge('action_dispatch.show_exceptions' => true, 'action_dispatch.logger' => ActiveSupport::Logger.new(output))
|
2011-11-24 14:37:48 -05:00
|
|
|
assert_response :error
|
|
|
|
output.rewind && err = output.read
|
|
|
|
assert err =~ /Error occurred while parsing request parameters/
|
2009-08-11 00:56:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-23 09:49:43 -05:00
|
|
|
test "occurring a parse error if parsing unsuccessful" do
|
|
|
|
with_test_routing do
|
|
|
|
begin
|
|
|
|
$stderr = StringIO.new # suppress the log
|
|
|
|
xml = "<person><name>David</name></pineapple>"
|
|
|
|
assert_raise(REXML::ParseException) { post "/parse", xml, default_headers.merge('action_dispatch.show_exceptions' => false) }
|
|
|
|
ensure
|
|
|
|
$stderr = STDERR
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-09 17:05:27 -05:00
|
|
|
test "parses multiple files" do
|
|
|
|
xml = <<-end_body
|
|
|
|
<person>
|
|
|
|
<name>David</name>
|
|
|
|
<avatars>
|
2012-01-01 12:57:55 -05:00
|
|
|
<avatar type='file' name='me.jpg' content_type='image/jpg'>#{::Base64.encode64('ABC')}</avatar>
|
|
|
|
<avatar type='file' name='you.gif' content_type='image/gif'>#{::Base64.encode64('DEF')}</avatar>
|
2009-01-09 17:05:27 -05:00
|
|
|
</avatars>
|
|
|
|
</person>
|
|
|
|
end_body
|
|
|
|
|
|
|
|
with_test_routing do
|
|
|
|
post "/parse", xml, default_headers
|
|
|
|
assert_response :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
person = TestController.last_request_parameters
|
|
|
|
|
|
|
|
assert_equal "image/jpg", person['person']['avatars']['avatar'].first.content_type
|
|
|
|
assert_equal "me.jpg", person['person']['avatars']['avatar'].first.original_filename
|
|
|
|
assert_equal "ABC", person['person']['avatars']['avatar'].first.read
|
|
|
|
|
|
|
|
assert_equal "image/gif", person['person']['avatars']['avatar'].last.content_type
|
|
|
|
assert_equal "you.gif", person['person']['avatars']['avatar'].last.original_filename
|
|
|
|
assert_equal "DEF", person['person']['avatars']['avatar'].last.read
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def with_test_routing
|
|
|
|
with_routing do |set|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2009-11-04 18:25:15 -05:00
|
|
|
match ':action', :to => ::XmlParamsParsingTest::TestController
|
2009-01-09 17:05:27 -05:00
|
|
|
end
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_headers
|
|
|
|
{'CONTENT_TYPE' => 'application/xml'}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class LegacyXmlParamsParsingTest < XmlParamsParsingTest
|
|
|
|
private
|
|
|
|
def default_headers
|
|
|
|
{'HTTP_X_POST_DATA_FORMAT' => 'xml'}
|
|
|
|
end
|
2009-11-04 18:25:15 -05:00
|
|
|
end
|
2011-04-28 04:56:11 -04:00
|
|
|
|
|
|
|
class RootLessXmlParamsParsingTest < ActionDispatch::IntegrationTest
|
|
|
|
class TestController < ActionController::Base
|
|
|
|
wrap_parameters :person, :format => :xml
|
|
|
|
|
|
|
|
class << self
|
|
|
|
attr_accessor :last_request_parameters
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse
|
|
|
|
self.class.last_request_parameters = request.request_parameters
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
TestController.last_request_parameters = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
test "parses hash params" do
|
|
|
|
with_test_routing do
|
|
|
|
xml = "<name>David</name>"
|
|
|
|
post "/parse", xml, {'CONTENT_TYPE' => 'application/xml'}
|
|
|
|
assert_response :ok
|
|
|
|
assert_equal({"name" => "David", "person" => {"name" => "David"}}, TestController.last_request_parameters)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def with_test_routing
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do
|
|
|
|
match ':action', :to => ::RootLessXmlParamsParsingTest::TestController
|
|
|
|
end
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|