Skip to content

Commit 27a14c9

Browse files
committed
Fix 500 error for xml format when method is not allowed
Problem: All requests to Not Allowed methods are returning 500 error instead of 405 when the format is xml. Cause: Route definitions for not allowed methods are returning an empty string as a response body. But the empty string fails to convert to xml and raises InvalidFormatter error. Solution: Define an exception for "method is not allowed" case. Return an error message "405 Not Allowed" instead of empty string.
1 parent 4ae8e0d commit 27a14c9

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
* [#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).
1212
* [#1255](https://github.com/ruby-grape/grape/pull/1255): Allow param type definition in `route_param` - [@namusyaka](https://github.com/namusyaka).
1313
* [#1257](https://github.com/ruby-grape/grape/pull/1257): Allow Proc, Symbol or String in `rescue_from with: ...` - [@namusyaka](https://github.com/namusyaka).
14-
* [#1282](https://github.com/ruby-grape/grape/pull/1282): Fix specs circular dependency - [@304](https://github.com/304).
1514
* Your contribution here.
1615

1716
#### Fixes
@@ -22,6 +21,8 @@
2221
* [#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).
2322
* [#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).
2423
* [#1270](https://github.com/ruby-grape/grape/pull/1270): Fix `param` versioning with a custom parameter - [@wshatch](https://github.com/wshatch).
24+
* [#1282](https://github.com/ruby-grape/grape/pull/1282): Fix specs circular dependency - [@304](https://github.com/304).
25+
* [#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).
2526

2627
0.14.0 (12/07/2015)
2728
===================

lib/grape.rb

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ module Exceptions
7272
autoload :InvalidMessageBody
7373
autoload :InvalidAcceptHeader
7474
autoload :InvalidVersionHeader
75+
autoload :MethodNotAllowed
7576
end
7677

7778
module ErrorFormatter

lib/grape/api.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,7 @@ def generate_not_allowed_method(path, allowed_methods, allow_header)
185185
return if not_allowed_methods.empty?
186186

187187
self.class.route(not_allowed_methods, path) do
188-
header 'Allow', allow_header
189-
status 405
190-
''
188+
fail Grape::Exceptions::MethodNotAllowed, header.merge('Allow' => allow_header)
191189
end
192190

193191
# move options endpoint to top of defined endpoints
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# encoding: utf-8
2+
module Grape
3+
module Exceptions
4+
class MethodNotAllowed < Base
5+
def initialize(headers)
6+
super(message: '405 Not Allowed', status: 405, headers: headers)
7+
end
8+
end
9+
end
10+
end

spec/grape/api_spec.rb

+21-3
Original file line numberDiff line numberDiff line change
@@ -523,10 +523,28 @@ def subject.enable_root_route!
523523
end
524524
put '/example'
525525
expect(last_response.status).to eql 405
526-
expect(last_response.body).to eql ''
526+
expect(last_response.body).to eql '405 Not Allowed'
527527
expect(last_response.headers['X-Custom-Header']).to eql 'foo'
528528
end
529529

530+
context 'when format is xml' do
531+
it 'returns a 405 for an unsupported method' do
532+
subject.format :xml
533+
subject.get 'example' do
534+
'example'
535+
end
536+
537+
put '/example'
538+
expect(last_response.status).to eql 405
539+
expect(last_response.body).to eq <<-XML
540+
<?xml version="1.0" encoding="UTF-8"?>
541+
<error>
542+
<message>405 Not Allowed</message>
543+
</error>
544+
XML
545+
end
546+
end
547+
530548
specify '405 responses includes an Allow header specifying supported methods' do
531549
subject.get 'example' do
532550
'example'
@@ -602,8 +620,8 @@ def subject.enable_root_route!
602620
expect(last_response.status).to eql 405
603621
end
604622

605-
it 'has an empty body' do
606-
expect(last_response.body).to be_blank
623+
it 'contains error message in body' do
624+
expect(last_response.body).to eq '405 Not Allowed'
607625
end
608626

609627
it 'has an Allow header' do

0 commit comments

Comments
 (0)