File tree Expand file tree Collapse file tree 2 files changed +11
-1
lines changed
Expand file tree Collapse file tree 2 files changed +11
-1
lines changed Original file line number Diff line number Diff line change 11require 'multi_json'
2+ require 'multi_xml'
23
34module Grape
45 module Middleware
@@ -57,7 +58,8 @@ module Formats
5758 :xml => :encode_xml
5859 }
5960 PARSERS = {
60- :json => :decode_json
61+ :json => :decode_json ,
62+ :xml => :decode_xml
6163 }
6264
6365 def formatters
@@ -122,6 +124,10 @@ def encode_txt(object)
122124 object . respond_to? ( :to_txt ) ? object . to_txt : object . to_s
123125 end
124126
127+ def decode_xml ( object )
128+ MultiXml . parse ( object )
129+ end
130+
125131 def encode_xml ( object )
126132 object . respond_to? ( :to_xml ) ? object . to_xml : object . to_s
127133 end
Original file line number Diff line number Diff line change @@ -141,6 +141,10 @@ def to_xml
141141 subject . env [ 'rack.request.form_hash' ] [ 'is_boolean' ] . should be_true
142142 subject . env [ 'rack.request.form_hash' ] [ 'string' ] . should == 'thing'
143143 end
144+ it 'should parse the body from an xml POST/PUT and put the contents into rack.request.from_hash' do
145+ subject . call ( { 'PATH_INFO' => '/info.xml' , 'Accept' => 'application/xml' , 'rack.input' => StringIO . new ( '<thing><name>Test</name></thing>' ) } )
146+ subject . env [ 'rack.request.form_hash' ] [ 'thing' ] [ 'name' ] . should == 'Test'
147+ end
144148 it 'should be able to fail gracefully if the body is regular POST content' do
145149 subject . call ( { 'PATH_INFO' => '/info' , 'Accept' => 'application/json' , 'rack.input' => StringIO . new ( 'name=Other+Test+Thing' ) } )
146150 subject . env [ 'rack.request.form_hash' ] . should be_nil
You can’t perform that action at this time.
0 commit comments