-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #906 from croeck/invalid-body-parse-error-not-resc…
…ued-by-handlers Invalid body parse error not rescued by handlers
- Loading branch information
Showing
8 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# encoding: utf-8 | ||
module Grape | ||
module Exceptions | ||
class InvalidMessageBody < Base | ||
def initialize(body_format) | ||
super(message: compose_message('invalid_message_body', body_format: body_format), status: 400) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
require 'spec_helper' | ||
|
||
describe Grape::Exceptions::ValidationErrors do | ||
context 'api with rescue_from :all handler' do | ||
subject { Class.new(Grape::API) } | ||
before { | ||
subject.rescue_from :all do |e| | ||
rack_response 'message was processed', 400 | ||
end | ||
subject.params do | ||
requires :beer | ||
end | ||
subject.post '/beer' do | ||
'beer received' | ||
end | ||
} | ||
|
||
def app | ||
subject | ||
end | ||
|
||
context 'with content_type json' do | ||
it 'can recover from failed body parsing' do | ||
post '/beer', 'test', 'CONTENT_TYPE' => 'application/json' | ||
expect(last_response.status).to eq 400 | ||
expect(last_response.body).to eq('message was processed') | ||
end | ||
end | ||
|
||
context 'with content_type xml' do | ||
it 'can recover from failed body parsing' do | ||
post '/beer', 'test', 'CONTENT_TYPE' => 'application/xml' | ||
expect(last_response.status).to eq 400 | ||
expect(last_response.body).to eq('message was processed') | ||
end | ||
end | ||
|
||
context 'with content_type text' do | ||
it 'can recover from failed body parsing' do | ||
post '/beer', 'test', 'CONTENT_TYPE' => 'text/plain' | ||
expect(last_response.status).to eq 400 | ||
expect(last_response.body).to eq('message was processed') | ||
end | ||
end | ||
|
||
context 'with no specific content_type' do | ||
it 'can recover from failed body parsing' do | ||
post '/beer', 'test', {} | ||
expect(last_response.status).to eq 400 | ||
expect(last_response.body).to eq('message was processed') | ||
end | ||
end | ||
end | ||
|
||
context 'api without a rescue handler' do | ||
subject { Class.new(Grape::API) } | ||
before { | ||
subject.params do | ||
requires :beer | ||
end | ||
subject.post '/beer' do | ||
'beer received' | ||
end | ||
} | ||
|
||
def app | ||
subject | ||
end | ||
|
||
context 'and with content_type json' do | ||
it 'can recover from failed body parsing' do | ||
post '/beer', 'test', 'CONTENT_TYPE' => 'application/json' | ||
expect(last_response.status).to eq 400 | ||
expect(last_response.body).to include('message body does not match declared format') | ||
expect(last_response.body).to include('application/json') | ||
end | ||
end | ||
|
||
context 'with content_type xml' do | ||
it 'can recover from failed body parsing' do | ||
post '/beer', 'test', 'CONTENT_TYPE' => 'application/xml' | ||
expect(last_response.status).to eq 400 | ||
expect(last_response.body).to include('message body does not match declared format') | ||
expect(last_response.body).to include('application/xml') | ||
end | ||
end | ||
|
||
context 'with content_type text' do | ||
it 'can recover from failed body parsing' do | ||
post '/beer', 'test', 'CONTENT_TYPE' => 'text/plain' | ||
expect(last_response.status).to eq 400 | ||
expect(last_response.body).to eq('beer is missing') | ||
end | ||
end | ||
|
||
context 'and with no specific content_type' do | ||
it 'can recover from failed body parsing' do | ||
post '/beer', 'test', {} | ||
expect(last_response.status).to eq 400 | ||
# plain response with text/html | ||
expect(last_response.body).to eq('beer is missing') | ||
end | ||
end | ||
end | ||
end |