Skip to content

Commit d1bba79

Browse files
committed
Redirect as plain text with optional message override.
1 parent 925a3fb commit d1bba79

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#### Fixes
1919

20+
* [#1194](https://github.com/ruby-grape/grape/pull/1194): Redirect as plain text with message - [@tylerdooling](https://github.com/tylerdooling).
2021
* [#1185](https://github.com/ruby-grape/grape/pull/1185): Use formatters for custom vendored content types - [@tylerdooling](https://github.com/tylerdooling).
2122
* [#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).
2223
* [#1153](https://github.com/ruby-grape/grape/pull/1153): Fixes boolean declaration in an external file - [@towanda](https://github.com/towanda).

UPGRADING.md

+9
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

+9-3
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

+11-2
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ def app
752752
get '/hey'
753753
expect(last_response.status).to eq 302
754754
expect(last_response.headers['Location']).to eq '/ha'
755-
expect(last_response.body).to eq ''
755+
expect(last_response.body).to eq 'This resource has been moved temporarily to /ha.'
756756
end
757757

758758
it 'has status code 303 if it is not get request and it is http 1.1' do
@@ -762,6 +762,7 @@ def app
762762
post '/hey', {}, 'HTTP_VERSION' => 'HTTP/1.1'
763763
expect(last_response.status).to eq 303
764764
expect(last_response.headers['Location']).to eq '/ha'
765+
expect(last_response.body).to eq 'An alternate resource is located at /ha.'
765766
end
766767

767768
it 'support permanent redirect' do
@@ -771,7 +772,15 @@ def app
771772
get '/hey'
772773
expect(last_response.status).to eq 301
773774
expect(last_response.headers['Location']).to eq '/ha'
774-
expect(last_response.body).to eq ''
775+
expect(last_response.body).to eq 'This resource has been moved permanently to /ha.'
776+
end
777+
778+
it 'allows for an optional redirect body override' do
779+
subject.get('/hey') do
780+
redirect '/ha', body: 'test body'
781+
end
782+
get '/hey'
783+
expect(last_response.body).to eq 'test body'
775784
end
776785
end
777786

0 commit comments

Comments
 (0)