Skip to content

Commit c1c4300

Browse files
committed
Merge pull request #1194 from tylerdooling/redirect-body
Redirect as plain text with message.
2 parents bd696b8 + d1bba79 commit c1c4300

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#### Fixes
2020

21+
* [#1194](https://github.com/ruby-grape/grape/pull/1194): Redirect as plain text with message - [@tylerdooling](https://github.com/tylerdooling).
2122
* [#1185](https://github.com/ruby-grape/grape/pull/1185): Use formatters for custom vendored content types - [@tylerdooling](https://github.com/tylerdooling).
2223
* [#1156](https://github.com/ruby-grape/grape/pull/1156): Fixed `no implicit conversion of Symbol into Integer` with nested `values` validation - [@quickpay](https://github.com/quickpay).
2324
* [#1153](https://github.com/ruby-grape/grape/pull/1153): Fixes boolean declaration in an external file - [@towanda](https://github.com/towanda).

UPGRADING.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ be bypassed when processing responses for status codes defined [by rack](https:/
2323

2424
See [#1190](https://github.com/ruby-grape/grape/pull/1190) for more information.
2525

26+
#### Redirects respond as plain text with message
27+
28+
`#redirect` now uses `text/plain` regardless of whether that format has
29+
been enabled. This prevents formatters from attempting to serialize the
30+
message body and allows for a descriptive message body to be provided - and
31+
optionally overridden - that better fulfills the theme of the HTTP spec.
32+
33+
See [#1194](https://github.com/ruby-grape/grape/pull/1194) for more information.
34+
2635
### Upgrading to >= 0.12.0
2736

2837
#### Changes in middleware

lib/grape/dsl/inside_route.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,25 @@ def error!(message, status = nil, headers = nil)
9191
# @param url [String] The url to be redirect.
9292
# @param options [Hash] The options used when redirect.
9393
# :permanent, default false.
94+
# :body, default a short message including the URL.
9495
def redirect(url, options = {})
95-
merged_options = options.reverse_merge(permanent: false)
96-
if merged_options[:permanent]
96+
permanent = options.fetch(:permanent, false)
97+
body_message = options.fetch(:body, nil)
98+
if permanent
9799
status 301
100+
body_message ||= "This resource has been moved permanently to #{url}."
98101
else
99102
if env[Grape::Http::Headers::HTTP_VERSION] == 'HTTP/1.1' && request.request_method.to_s.upcase != Grape::Http::Headers::GET
100103
status 303
104+
body_message ||= "An alternate resource is located at #{url}."
101105
else
102106
status 302
107+
body_message ||= "This resource has been moved temporarily to #{url}."
103108
end
104109
end
105110
header 'Location', url
106-
body ''
111+
content_type 'text/plain'
112+
body body_message
107113
end
108114

109115
# Set or retrieve the HTTP status code.

spec/grape/endpoint_spec.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ def app
774774
get '/hey'
775775
expect(last_response.status).to eq 302
776776
expect(last_response.headers['Location']).to eq '/ha'
777-
expect(last_response.body).to eq ''
777+
expect(last_response.body).to eq 'This resource has been moved temporarily to /ha.'
778778
end
779779

780780
it 'has status code 303 if it is not get request and it is http 1.1' do
@@ -784,6 +784,7 @@ def app
784784
post '/hey', {}, 'HTTP_VERSION' => 'HTTP/1.1'
785785
expect(last_response.status).to eq 303
786786
expect(last_response.headers['Location']).to eq '/ha'
787+
expect(last_response.body).to eq 'An alternate resource is located at /ha.'
787788
end
788789

789790
it 'support permanent redirect' do
@@ -793,7 +794,15 @@ def app
793794
get '/hey'
794795
expect(last_response.status).to eq 301
795796
expect(last_response.headers['Location']).to eq '/ha'
796-
expect(last_response.body).to eq ''
797+
expect(last_response.body).to eq 'This resource has been moved permanently to /ha.'
798+
end
799+
800+
it 'allows for an optional redirect body override' do
801+
subject.get('/hey') do
802+
redirect '/ha', body: 'test body'
803+
end
804+
get '/hey'
805+
expect(last_response.body).to eq 'test body'
797806
end
798807
end
799808

0 commit comments

Comments
 (0)