Skip to content

Commit d2e2f7e

Browse files
author
Josh Murphy
committed
Add tests for returning a 415 when content type isn't supported
1 parent d571f25 commit d2e2f7e

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

spec/grape/middleware/formatter_spec.rb

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'spec_helper'
2+
require 'pry'
23

34
describe Grape::Middleware::Formatter do
45
subject { Grape::Middleware::Formatter.new(app) }
@@ -224,6 +225,80 @@ def to_xml
224225

225226
context 'input' do
226227
%w[POST PATCH PUT DELETE].each do |method|
228+
context 'when body is not nil or empty' do
229+
context 'when Content-Type is supported' do
230+
let(:io) { StringIO.new('{"is_boolean":true,"string":"thing"}') }
231+
let(:content_type) { 'application/json' }
232+
233+
it "parses the body from #{method} and copies values into rack.request.form_hash" do
234+
subject.call(
235+
'PATH_INFO' => '/info',
236+
'REQUEST_METHOD' => method,
237+
'CONTENT_TYPE' => content_type,
238+
'rack.input' => io,
239+
'CONTENT_LENGTH' => io.length
240+
)
241+
expect(subject.env['rack.request.form_hash']['is_boolean']).to be true
242+
expect(subject.env['rack.request.form_hash']['string']).to eq('thing')
243+
end
244+
end
245+
246+
context 'when Content-Type is not supported' do
247+
let(:io) { StringIO.new('{"is_boolean":true,"string":"thing"}') }
248+
let(:content_type) { 'application/atom+xml' }
249+
250+
it 'returns a 415 HTTP error status' do
251+
error = catch(:error) {
252+
subject.call(
253+
'PATH_INFO' => '/info',
254+
'REQUEST_METHOD' => method,
255+
'CONTENT_TYPE' => content_type,
256+
'rack.input' => io,
257+
'CONTENT_LENGTH' => io.length
258+
)
259+
}
260+
expect(error[:status]).to eq(415)
261+
expect(error[:message]).to eq("The requested content-type 'application/atom+xml' is not supported.")
262+
end
263+
end
264+
end
265+
266+
context 'when body is nil' do
267+
let(:io) { double }
268+
before do
269+
allow(io).to receive_message_chain(:rewind, :read).and_return(nil)
270+
end
271+
272+
it 'does not read and parse the body' do
273+
expect(subject).not_to receive(:read_rack_input)
274+
subject.call(
275+
'PATH_INFO' => '/info',
276+
'REQUEST_METHOD' => method,
277+
'CONTENT_TYPE' => 'application/json',
278+
'rack.input' => io,
279+
'CONTENT_LENGTH' => 0
280+
)
281+
end
282+
end
283+
284+
context 'when body is empty' do
285+
let(:io) { double }
286+
before do
287+
allow(io).to receive_message_chain(:rewind, :read).and_return('')
288+
end
289+
290+
it 'does not read and parse the body' do
291+
expect(subject).not_to receive(:read_rack_input)
292+
subject.call(
293+
'PATH_INFO' => '/info',
294+
'REQUEST_METHOD' => method,
295+
'CONTENT_TYPE' => 'application/json',
296+
'rack.input' => io,
297+
'CONTENT_LENGTH' => 0
298+
)
299+
end
300+
end
301+
227302
['application/json', 'application/json; charset=utf-8'].each do |content_type|
228303
context content_type do
229304
it "parses the body from #{method} and copies values into rack.request.form_hash" do

0 commit comments

Comments
 (0)