|
1 | 1 | require 'spec_helper'
|
| 2 | +require 'pry' |
2 | 3 |
|
3 | 4 | describe Grape::Middleware::Formatter do
|
4 | 5 | subject { Grape::Middleware::Formatter.new(app) }
|
@@ -224,6 +225,80 @@ def to_xml
|
224 | 225 |
|
225 | 226 | context 'input' do
|
226 | 227 | %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 | + |
227 | 302 | ['application/json', 'application/json; charset=utf-8'].each do |content_type|
|
228 | 303 | context content_type do
|
229 | 304 | it "parses the body from #{method} and copies values into rack.request.form_hash" do
|
|
0 commit comments