Skip to content

Commit 5ebb503

Browse files
committed
Redirect as plain text with message.
1 parent 925a3fb commit 5ebb503

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
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 that
31+
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

+5-1
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,19 @@ def redirect(url, options = {})
9595
merged_options = options.reverse_merge(permanent: false)
9696
if merged_options[:permanent]
9797
status 301
98+
message = "This resource has been moved permanently to #{url}"
9899
else
99100
if env[Grape::Http::Headers::HTTP_VERSION] == 'HTTP/1.1' && request.request_method.to_s.upcase != Grape::Http::Headers::GET
100101
status 303
102+
message = "An alternate resource is located at #{url}"
101103
else
102104
status 302
105+
message = "This resource has been moved temporarily to #{url}"
103106
end
104107
end
105108
header 'Location', url
106-
body ''
109+
content_type 'text/plain'
110+
body message
107111
end
108112

109113
# Set or retrieve the HTTP status code.

spec/grape/endpoint_spec.rb

+3-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,7 @@ 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'
775776
end
776777
end
777778

0 commit comments

Comments
 (0)