Skip to content

Commit 257861b

Browse files
committed
sets 204 as default status for delete
- adds changelog entry - addresses comments
1 parent 152fa40 commit 257861b

File tree

6 files changed

+78
-21
lines changed

6 files changed

+78
-21
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Next Release
55

66
* [#1503](https://github.com/ruby-grape/grape/pull/1503): Allow to use regexp validator with arrays - [@akoltun](https://github.com/akoltun).
77
* [#1507](https://github.com/ruby-grape/grape/pull/1507): Add group attributes for parameter definitions - [@304](https://github.com/304).
8+
* [#1532](https://github.com/ruby-grape/grape/pull/1532): Sets 204 as default status for delete - [@LeFnord](https://github.com/LeFnord).
89
* Your contribution here.
910

1011
#### Fixes
@@ -15,7 +16,7 @@ Next Release
1516
* [#1510](https://github.com/ruby-grape/grape/pull/1510): Fix: inconsistent validation for multiple parameters - [@dgasper](https://github.com/dgasper).
1617
* [#1526](https://github.com/ruby-grape/grape/pull/1526): Reduce warnings caused by instance variables not initialized - [@cpetschnig](https://github.com/cpetschnig).
1718
* [#1531](https://github.com/ruby-grape/grape/pull/1531): Updates gem dependencies - [@LeFnord](https://github.com/LeFnord).
18-
19+
* Your contribution here.
1920

2021
0.18.0 (10/7/2016)
2122
==================

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1741,7 +1741,7 @@ cookies.delete :status_count, path: '/'
17411741

17421742
## HTTP Status Code
17431743

1744-
By default Grape returns a 200 status code for `GET`-Requests and 201 for `POST`-Requests.
1744+
By default Grape returns a 201 for `POST`-Requests, 204 for `DELETE`-Requests and 200 status code for all other Requests.
17451745
You can use `status` to query and set the actual HTTP Status Code
17461746

17471747
```ruby

UPGRADING.md

+15
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ Prior to this version the response would be `one is missing`.
4141

4242
See [#1510](https://github.com/ruby-grape/grape/pull/1510) for more information.
4343

44+
#### The default status code for DELETE is now 204 instead of 200.
45+
46+
Breaking change: Sets the default response status code for a delete request to 204.
47+
A status of 204 makes the response more distinguishable and therefore easier to handle on the client side, particularly because a DELETE request typically returns an empty body as the resource was deleted or voided.
48+
49+
To achieve the old behavior, one has to set it explicitly:
50+
```ruby
51+
delete :id do
52+
status 200
53+
'foo successfully deleted'
54+
end
55+
```
56+
57+
For more information see: [#1532](https://github.com/ruby-grape/grape/pull/1532).
58+
4459
### Upgrading to >= 0.17.0
4560

4661
#### Removed official support for Ruby < 2.2.2

lib/grape/dsl/inside_route.rb

+2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ def status(status = nil)
129129
case request.request_method.to_s.upcase
130130
when Grape::Http::Headers::POST
131131
201
132+
when Grape::Http::Headers::DELETE
133+
204
132134
else
133135
200
134136
end

spec/grape/dsl/inside_route_spec.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def initialize
9191
end
9292

9393
describe '#status' do
94-
%w(GET PUT DELETE OPTIONS).each do |method|
94+
%w(GET PUT OPTIONS).each do |method|
9595
it 'defaults to 200 on GET' do
9696
request = Grape::Request.new(Rack::MockRequest.env_for('/', method: method))
9797
expect(subject).to receive(:request).and_return(request)
@@ -105,6 +105,12 @@ def initialize
105105
expect(subject.status).to eq 201
106106
end
107107

108+
it 'defaults to 204 on DELETE' do
109+
request = Grape::Request.new(Rack::MockRequest.env_for('/', method: 'DELETE'))
110+
expect(subject).to receive(:request).and_return(request)
111+
expect(subject.status).to eq 204
112+
end
113+
108114
it 'returns status set' do
109115
subject.status 501
110116
expect(subject.status).to eq 501

spec/grape/endpoint_spec.rb

+51-18
Original file line numberDiff line numberDiff line change
@@ -1095,32 +1095,65 @@ def memoized
10951095
end
10961096

10971097
context 'anchoring' do
1098-
verbs = %w(post get head delete put options patch)
1098+
describe 'delete 204' do
1099+
it 'allows for the anchoring option with a delete method' do
1100+
subject.send(:delete, '/example', anchor: true) {}
1101+
send(:delete, '/example/and/some/more')
1102+
expect(last_response.status).to eql 404
1103+
end
10991104

1100-
verbs.each do |verb|
1101-
it "allows for the anchoring option with a #{verb.upcase} method" do
1102-
subject.send(verb, '/example', anchor: true) do
1103-
verb
1104-
end
1105-
send(verb, '/example/and/some/more')
1105+
it 'anchors paths by default for the delete method' do
1106+
subject.send(:delete, '/example') {}
1107+
send(:delete, '/example/and/some/more')
11061108
expect(last_response.status).to eql 404
11071109
end
11081110

1109-
it "anchors paths by default for the #{verb.upcase} method" do
1110-
subject.send(verb, '/example') do
1111-
verb
1111+
it 'responds to /example/and/some/more for the non-anchored delete method' do
1112+
subject.send(:delete, '/example', anchor: false) {}
1113+
send(:delete, '/example/and/some/more')
1114+
expect(last_response.status).to eql 204
1115+
expect(last_response.body).to be_empty
1116+
end
1117+
end
1118+
1119+
describe 'delete 200, with response body' do
1120+
it 'responds to /example/and/some/more for the non-anchored delete method' do
1121+
subject.send(:delete, '/example', anchor: false) do
1122+
status 200
1123+
body 'deleted'
11121124
end
1113-
send(verb, '/example/and/some/more')
1114-
expect(last_response.status).to eql 404
1125+
send(:delete, '/example/and/some/more')
1126+
expect(last_response.status).to eql 200
1127+
expect(last_response.body).not_to be_empty
11151128
end
1129+
end
11161130

1117-
it "responds to /example/and/some/more for the non-anchored #{verb.upcase} method" do
1118-
subject.send(verb, '/example', anchor: false) do
1119-
verb
1131+
describe 'all other' do
1132+
%w(post get head put options patch).each do |verb|
1133+
it "allows for the anchoring option with a #{verb.upcase} method" do
1134+
subject.send(verb, '/example', anchor: true) do
1135+
verb
1136+
end
1137+
send(verb, '/example/and/some/more')
1138+
expect(last_response.status).to eql 404
1139+
end
1140+
1141+
it "anchors paths by default for the #{verb.upcase} method" do
1142+
subject.send(verb, '/example') do
1143+
verb
1144+
end
1145+
send(verb, '/example/and/some/more')
1146+
expect(last_response.status).to eql 404
1147+
end
1148+
1149+
it "responds to /example/and/some/more for the non-anchored #{verb.upcase} method" do
1150+
subject.send(verb, '/example', anchor: false) do
1151+
verb
1152+
end
1153+
send(verb, '/example/and/some/more')
1154+
expect(last_response.status).to eql verb == 'post' ? 201 : 200
1155+
expect(last_response.body).to eql verb == 'head' ? '' : verb
11201156
end
1121-
send(verb, '/example/and/some/more')
1122-
expect(last_response.status).to eql verb == 'post' ? 201 : 200
1123-
expect(last_response.body).to eql verb == 'head' ? '' : verb
11241157
end
11251158
end
11261159
end

0 commit comments

Comments
 (0)