Skip to content

Change HTTP DELETE status code and body logic, use nil for HTTP 204 #1774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ def status(status = nil)
when Grape::Http::Headers::POST
201
when Grape::Http::Headers::DELETE
if @body.present?
200
else
if @body.nil?
204
else
200
end
else
200
Expand Down Expand Up @@ -222,12 +222,17 @@ def cookies
# end
#
# GET /body # => "Body"
def body(value = nil)
if value
@body = value
elsif value == false
@body = ''
status 204
def body(*value)
raise ArgumentError 'you can only set a body with one argument' if value.length > 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the text is not required, just raise ArgumentError.

What if value is nil, what's nil.length?

This needs a test.

You can also turn this into a separate PR if you want.


if value.length == 1
value = value.first
if value.nil?
@body = ''
status 204
else
@body = value
end
else
@body
end
Expand All @@ -244,7 +249,7 @@ def body(value = nil)
# DELETE /12 # => 204 No Content, ""
def return_no_content
status 204
body false
body nil
end

# Allows you to define the response as a file-like object.
Expand Down
4 changes: 2 additions & 2 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3553,10 +3553,10 @@ def before
end

context 'body' do
context 'false' do
context 'nil' do
before do
subject.get '/blank' do
body false
body nil
end
end
it 'returns blank body' do
Expand Down
45 changes: 38 additions & 7 deletions spec/grape/dsl/inside_route_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,42 @@ def initialize
expect(subject.status).to eq 204
end

it 'defaults to 200 on DELETE with a body present' do
request = Grape::Request.new(Rack::MockRequest.env_for('/', method: 'DELETE'))
subject.body 'content here'
expect(subject).to receive(:request).and_return(request)
expect(subject.status).to eq 200
describe 'what is regarded as content on DELETE' do
it 'regards a string as content' do
request = Grape::Request.new(Rack::MockRequest.env_for('/', method: 'DELETE'))
subject.body 'content here'
expect(subject).to receive(:request).and_return(request)
expect(subject.status).to eq 200
end

it 'regards an empty string as content' do
request = Grape::Request.new(Rack::MockRequest.env_for('/', method: 'DELETE'))
subject.body ''
expect(subject).to receive(:request).and_return(request)
expect(subject.status).to eq 200
end

it 'regards an empty hash as content' do
request = Grape::Request.new(Rack::MockRequest.env_for('/', method: 'DELETE'))
subject.body({}) # Use brackets, because it's a block otherwise
expect(subject).to receive(:request).and_return(request)
expect(subject.status).to eq 200
end

it 'regards an empty array as content' do
request = Grape::Request.new(Rack::MockRequest.env_for('/', method: 'DELETE'))
subject.body []
expect(subject).to receive(:request).and_return(request)
expect(subject.status).to eq 200
end

# Still failing for some weird kind of reason.
it 'regards a nil as no content' do
request = Grape::Request.new(Rack::MockRequest.env_for('/', method: 'DELETE'))
subject.body nil
expect(subject).to receive(:request).and_return(request)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test doesn't work because there is wrong code in using expect ~ receive.
Depending on the implementation you modified, in this case the status method is called internally.
As a result, the @status variable is assigned and the request member isn't called and it ends.

You don't need test this expectation. Please remove this line.

expect(subject.status).to eq 204
end
end

it 'returns status set' do
Expand Down Expand Up @@ -184,9 +215,9 @@ def initialize
end
end

describe 'false' do
describe 'nil' do
before do
subject.body false
subject.body nil
end

it 'sets status to 204' do
Expand Down
10 changes: 5 additions & 5 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1320,18 +1320,18 @@ def memoized
end
end

describe 'delete 204, with nil has return value (no explicit body)' do
describe 'delete 200, with empty string as return value' do
it 'responds to /example delete method' do
subject.delete(:example) { nil }
subject.delete(:example) { '' }
delete '/example'
expect(last_response.status).to eql 204
expect(last_response.status).to eql 200
expect(last_response.body).to be_empty
end
end

describe 'delete 204, with empty array has return value (no explicit body)' do
describe 'delete 204, with nil as return value (no explicit body)' do
it 'responds to /example delete method' do
subject.delete(:example) { '' }
subject.delete(:example) { nil }
delete '/example'
expect(last_response.status).to eql 204
expect(last_response.body).to be_empty
Expand Down