spec/grape/middleware/formatter_spec.rb in grape-0.2.6 vs spec/grape/middleware/formatter_spec.rb in grape-0.3.0
- old
+ new
@@ -2,11 +2,11 @@
describe Grape::Middleware::Formatter do
subject{ Grape::Middleware::Formatter.new(app) }
before{ subject.stub!(:dup).and_return(subject) }
- let(:app){ lambda{|env| [200, {}, [@body]]} }
+ let(:app){ lambda{|env| [200, {}, [@body || { "foo" => "bar" }]]} }
context 'serialization' do
it 'looks at the bodies for possibly serializable data' do
@body = {"abc" => "def"}
status, headers, bodies = *subject.call({'PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json'})
@@ -35,21 +35,22 @@
subject.call({'PATH_INFO' => '/somewhere.xml', 'HTTP_ACCEPT' => 'application/json'}).last.each{|b| b.should == '<bar/>'}
end
end
context 'detection' do
+
it 'uses the extension if one is provided' do
subject.call({'PATH_INFO' => '/info.xml'})
subject.env['api.format'].should == :xml
subject.call({'PATH_INFO' => '/info.json'})
subject.env['api.format'].should == :json
end
it 'uses the format parameter if one is provided' do
- subject.call({'PATH_INFO' => '/somewhere','QUERY_STRING' => 'format=json'})
+ subject.call({'PATH_INFO' => '/info','QUERY_STRING' => 'format=json'})
subject.env['api.format'].should == :json
- subject.call({'PATH_INFO' => '/somewhere','QUERY_STRING' => 'format=xml'})
+ subject.call({'PATH_INFO' => '/info','QUERY_STRING' => 'format=xml'})
subject.env['api.format'].should == :xml
end
it 'uses the default format if none is provided' do
subject.call({'PATH_INFO' => '/info'})
@@ -146,45 +147,50 @@
body.body.should == ['CUSTOM JSON FORMAT']
end
end
context 'input' do
- [ "application/json", "application/json; charset=utf-8" ].each do |content_type|
- it 'parses the body from a POST/PUT and put the contents into rack.request.form_hash for #{content_type}' do
- io = StringIO.new('{"is_boolean":true,"string":"thing"}')
- subject.call({
- 'PATH_INFO' => '/info',
- 'REQUEST_METHOD' => 'POST',
- 'CONTENT_TYPE' => content_type,
- 'rack.input' => io,
- 'CONTENT_LENGTH' => io.length
- })
- subject.env['rack.request.form_hash']['is_boolean'].should be_true
- subject.env['rack.request.form_hash']['string'].should == 'thing'
+ [ "POST", "PATCH", "PUT" ].each do |method|
+ [ "application/json", "application/json; charset=utf-8" ].each do |content_type|
+ context content_type do
+ it 'parses the body from #{method} and copies values into rack.request.form_hash' do
+ io = StringIO.new('{"is_boolean":true,"string":"thing"}')
+ subject.call({
+ 'PATH_INFO' => '/info',
+ 'REQUEST_METHOD' => method,
+ 'CONTENT_TYPE' => content_type,
+ 'rack.input' => io,
+ 'CONTENT_LENGTH' => io.length
+ })
+ subject.env['rack.request.form_hash']['is_boolean'].should be_true
+ subject.env['rack.request.form_hash']['string'].should == 'thing'
+ end
+ end
end
- end
- it 'parses the body from an xml POST/PUT and put the contents into rack.request.from_hash' do
- io = StringIO.new('<thing><name>Test</name></thing>')
- subject.call({
- 'PATH_INFO' => '/info.xml',
- 'REQUEST_METHOD' => 'POST',
- 'CONTENT_TYPE' => 'application/xml',
- 'rack.input' => io,
- 'CONTENT_LENGTH' => io.length
- })
- subject.env['rack.request.form_hash']['thing']['name'].should == 'Test'
- end
- [ Rack::Request::FORM_DATA_MEDIA_TYPES, Rack::Request::PARSEABLE_DATA_MEDIA_TYPES ].flatten.each do |content_type|
- it "ignores #{content_type}" do
- io = StringIO.new('name=Other+Test+Thing')
+ it 'parses the body from an xml #{method} and copies values into rack.request.from_hash' do
+ io = StringIO.new('<thing><name>Test</name></thing>')
subject.call({
- 'PATH_INFO' => '/info',
- 'REQUEST_METHOD' => 'POST',
- 'CONTENT_TYPE' => content_type,
+ 'PATH_INFO' => '/info.xml',
+ 'REQUEST_METHOD' => method,
+ 'CONTENT_TYPE' => 'application/xml',
'rack.input' => io,
'CONTENT_LENGTH' => io.length
})
- subject.env['rack.request.form_hash'].should be_nil
+ subject.env['rack.request.form_hash']['thing']['name'].should == 'Test'
end
+ [ Rack::Request::FORM_DATA_MEDIA_TYPES, Rack::Request::PARSEABLE_DATA_MEDIA_TYPES ].flatten.each do |content_type|
+ it "ignores #{content_type}" do
+ io = StringIO.new('name=Other+Test+Thing')
+ subject.call({
+ 'PATH_INFO' => '/info',
+ 'REQUEST_METHOD' => method,
+ 'CONTENT_TYPE' => content_type,
+ 'rack.input' => io,
+ 'CONTENT_LENGTH' => io.length
+ })
+ subject.env['rack.request.form_hash'].should be_nil
+ end
+ end
end
end
+
end