Skip to content

Fix 500 error for xml format when method is not allowed #1283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
* [#1252](https://github.com/ruby-grape/grape/pull/1252): Allow default to be a subset or equal to allowed values without raising IncompatibleOptionValues - [@jeradphelps](https://github.com/jeradphelps).
* [#1255](https://github.com/ruby-grape/grape/pull/1255): Allow param type definition in `route_param` - [@namusyaka](https://github.com/namusyaka).
* [#1257](https://github.com/ruby-grape/grape/pull/1257): Allow Proc, Symbol or String in `rescue_from with: ...` - [@namusyaka](https://github.com/namusyaka).
* [#1282](https://github.com/ruby-grape/grape/pull/1282): Fix specs circular dependency - [@304](https://github.com/304).
* Your contribution here.

#### Fixes
Expand All @@ -22,6 +21,8 @@
* [#1263](https://github.com/ruby-grape/grape/pull/1263): Fix `route :any, '*path'` breaking generated `OPTIONS`, Method Not Allowed routes - [@arempe93](https://github.com/arempe93).
* [#1266](https://github.com/ruby-grape/grape/pull/1266): Fix `Allow` header including `OPTIONS` when `do_not_route_options!` is active - [@arempe93](https://github.com/arempe93).
* [#1270](https://github.com/ruby-grape/grape/pull/1270): Fix `param` versioning with a custom parameter - [@wshatch](https://github.com/wshatch).
* [#1282](https://github.com/ruby-grape/grape/pull/1282): Fix specs circular dependency - [@304](https://github.com/304).
* [#1283](https://github.com/ruby-grape/grape/pull/1283): Fix 500 error for xml format when method is not allowed - [@304](https://github.com/304).

0.14.0 (12/07/2015)
===================
Expand Down
1 change: 1 addition & 0 deletions lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ module Exceptions
autoload :InvalidMessageBody
autoload :InvalidAcceptHeader
autoload :InvalidVersionHeader
autoload :MethodNotAllowed
end

module ErrorFormatter
Expand Down
4 changes: 1 addition & 3 deletions lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,7 @@ def generate_not_allowed_method(path, allowed_methods, allow_header)
return if not_allowed_methods.empty?

self.class.route(not_allowed_methods, path) do
header 'Allow', allow_header
status 405
''
fail Grape::Exceptions::MethodNotAllowed, header.merge('Allow' => allow_header)
end

# move options endpoint to top of defined endpoints
Expand Down
10 changes: 10 additions & 0 deletions lib/grape/exceptions/method_not_allowed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# encoding: utf-8
module Grape
module Exceptions
class MethodNotAllowed < Base
def initialize(headers)
super(message: '405 Not Allowed', status: 405, headers: headers)
end
end
end
end
24 changes: 21 additions & 3 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -523,10 +523,28 @@ def subject.enable_root_route!
end
put '/example'
expect(last_response.status).to eql 405
expect(last_response.body).to eql ''
expect(last_response.body).to eql '405 Not Allowed'
expect(last_response.headers['X-Custom-Header']).to eql 'foo'
end

context 'when format is xml' do
it 'returns a 405 for an unsupported method' do
subject.format :xml
subject.get 'example' do
'example'
end

put '/example'
expect(last_response.status).to eql 405
expect(last_response.body).to eq <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<error>
<message>405 Not Allowed</message>
</error>
XML
end
end

specify '405 responses includes an Allow header specifying supported methods' do
subject.get 'example' do
'example'
Expand Down Expand Up @@ -602,8 +620,8 @@ def subject.enable_root_route!
expect(last_response.status).to eql 405
end

it 'has an empty body' do
expect(last_response.body).to be_blank
it 'contains error message in body' do
expect(last_response.body).to eq '405 Not Allowed'
end

it 'has an Allow header' do
Expand Down