-
-
Notifications
You must be signed in to change notification settings - Fork 284
Add Rails/UnprocessableContentStatus
cop
#1520
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
a4824e5
cbd4725
c5c2d7a
af2ea77
f7ec81e
c835727
55f1f6c
76b07f6
ff94df5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#1520](https://github.com/rubocop/rubocop-rails/pull/1520): New `Rails/DeprecatedHttpStatusNames` cop. ([@tuxagon][]) |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,81 @@ | ||||||
# frozen_string_literal: true | ||||||
|
||||||
module RuboCop | ||||||
module Cop | ||||||
module Rails | ||||||
# Enforces the use of the current HTTP status names instead of deprecated ones. | ||||||
|
# Enforces the use of the current HTTP status names instead of deprecated ones. | |
# Enforces consistency by using the current HTTP status names. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the warning has disappeared, how about naming it as follows?
class DeprecatedHttpStatusNames < Base | |
class HttpStatusNameConsistency < Base |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also rename these constant and method names?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@koic I updated the constant to be PREFERRED_STATUSES
. Did you have another name in mind?
As for RESTRICT_ON_SEND
, what's the issue with that name? I used the same naming as the one in RuboCop::Cop::Rails::HttpStatus
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RESTRICT_ON_SEND
is fine; find_deprecated_status_names
should be renamed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see because of the deprecated
mention. I renamed the method check_status_name_consistency
. I removed the find
part since the result of each call isn't used.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can be made into a slightly simpler message.
MSG = 'Use `:%<preferred>s` instead of `:%<deprecated>s`. The `:%<deprecated>s` status is deprecated.' | |
MSG = 'Prefer `:%<preferred>s` over `:%<deprecated>s`.' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::DeprecatedHttpStatusNames, :config do | ||
context 'when Rack is older than 3.1' do | ||
let(:gem_versions) { { 'rack' => '3.0.0' } } | ||
|
||
it 'does nothing' do | ||
expect_no_offenses(<<~RUBY) | ||
render json: { error: 'Invalid data' }, status: :unprocessable_entity | ||
head :payload_too_large | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when Rack is 3.1 or later' do | ||
let(:gem_versions) { { 'rack' => '3.1.0' } } | ||
|
||
context 'with :unprocessable_entity' do | ||
it 'registers an offense when using :unprocessable_entity in render' do | ||
expect_offense(<<~RUBY) | ||
render json: { error: 'Invalid data' }, status: :unprocessable_entity | ||
^^^^^^^^^^^^^^^^^^^^^ Use `:unprocessable_content` instead of `:unprocessable_entity`. The `:unprocessable_entity` status is deprecated. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
render json: { error: 'Invalid data' }, status: :unprocessable_content | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using :unprocessable_entity in head' do | ||
expect_offense(<<~RUBY) | ||
head :unprocessable_entity | ||
^^^^^^^^^^^^^^^^^^^^^ Use `:unprocessable_content` instead of `:unprocessable_entity`. The `:unprocessable_entity` status is deprecated. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
head :unprocessable_content | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using :unprocessable_entity in redirect_to' do | ||
expect_offense(<<~RUBY) | ||
redirect_to some_path, status: :unprocessable_entity | ||
^^^^^^^^^^^^^^^^^^^^^ Use `:unprocessable_content` instead of `:unprocessable_entity`. The `:unprocessable_entity` status is deprecated. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
redirect_to some_path, status: :unprocessable_content | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using :unprocessable_entity in assert_response' do | ||
expect_offense(<<~RUBY) | ||
assert_response :unprocessable_entity | ||
^^^^^^^^^^^^^^^^^^^^^ Use `:unprocessable_content` instead of `:unprocessable_entity`. The `:unprocessable_entity` status is deprecated. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
assert_response :unprocessable_content | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using :unprocessable_entity in assert_redirected_to' do | ||
expect_offense(<<~RUBY) | ||
assert_redirected_to some_path, status: :unprocessable_entity | ||
^^^^^^^^^^^^^^^^^^^^^ Use `:unprocessable_content` instead of `:unprocessable_entity`. The `:unprocessable_entity` status is deprecated. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
assert_redirected_to some_path, status: :unprocessable_content | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using :unprocessable_entity in ternary expression' do | ||
expect_offense(<<~RUBY) | ||
render json: { error: 'Invalid data' }, status: some_condition ? :unprocessable_entity : :ok | ||
^^^^^^^^^^^^^^^^^^^^^ Use `:unprocessable_content` instead of `:unprocessable_entity`. The `:unprocessable_entity` status is deprecated. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
render json: { error: 'Invalid data' }, status: some_condition ? :unprocessable_content : :ok | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using :unprocessable_content' do | ||
expect_no_offenses(<<~RUBY) | ||
render json: { error: 'Invalid data' }, status: :unprocessable_content | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using :unprocessable_entity in hash key' do | ||
expect_no_offenses(<<~RUBY) | ||
{ unprocessable_entity: 'Invalid data' } | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with :payload_too_large' do | ||
it 'registers an offense when using :payload_too_large in render' do | ||
expect_offense(<<~RUBY) | ||
render json: { error: 'File too big' }, status: :payload_too_large | ||
^^^^^^^^^^^^^^^^^^ Use `:content_too_large` instead of `:payload_too_large`. The `:payload_too_large` status is deprecated. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
render json: { error: 'File too big' }, status: :content_too_large | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using :payload_too_large in head' do | ||
expect_offense(<<~RUBY) | ||
head :payload_too_large | ||
^^^^^^^^^^^^^^^^^^ Use `:content_too_large` instead of `:payload_too_large`. The `:payload_too_large` status is deprecated. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
head :content_too_large | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using :payload_too_large in redirect_to' do | ||
expect_offense(<<~RUBY) | ||
redirect_to some_path, status: :payload_too_large | ||
^^^^^^^^^^^^^^^^^^ Use `:content_too_large` instead of `:payload_too_large`. The `:payload_too_large` status is deprecated. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
redirect_to some_path, status: :content_too_large | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using :payload_too_large in assert_response' do | ||
expect_offense(<<~RUBY) | ||
assert_response :payload_too_large | ||
^^^^^^^^^^^^^^^^^^ Use `:content_too_large` instead of `:payload_too_large`. The `:payload_too_large` status is deprecated. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
assert_response :content_too_large | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using :payload_too_large in assert_redirected_to' do | ||
expect_offense(<<~RUBY) | ||
assert_redirected_to some_path, status: :payload_too_large | ||
^^^^^^^^^^^^^^^^^^ Use `:content_too_large` instead of `:payload_too_large`. The `:payload_too_large` status is deprecated. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
assert_redirected_to some_path, status: :content_too_large | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using :payload_too_large in ternary expression' do | ||
expect_offense(<<~RUBY) | ||
render json: { error: 'File too big' }, status: some_condition ? :payload_too_large : :ok | ||
^^^^^^^^^^^^^^^^^^ Use `:content_too_large` instead of `:payload_too_large`. The `:payload_too_large` status is deprecated. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
render json: { error: 'File too big' }, status: some_condition ? :content_too_large : :ok | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using :content_too_large' do | ||
expect_no_offenses(<<~RUBY) | ||
render json: { error: 'File too big' }, status: :content_too_large | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using :payload_too_large in hash key' do | ||
expect_no_offenses(<<~RUBY) | ||
{ payload_too_large: 'File too big' } | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with mixed deprecated statuses' do | ||
it 'handles multiple deprecated statuses in the same code' do | ||
expect_offense(<<~RUBY) | ||
head :unprocessable_entity | ||
^^^^^^^^^^^^^^^^^^^^^ Use `:unprocessable_content` instead of `:unprocessable_entity`. The `:unprocessable_entity` status is deprecated. | ||
head :payload_too_large | ||
^^^^^^^^^^^^^^^^^^ Use `:content_too_large` instead of `:payload_too_large`. The `:payload_too_large` status is deprecated. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
head :unprocessable_content | ||
head :content_too_large | ||
RUBY | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.