Skip to content

Commit

Permalink
Merge pull request #1267 from tldn0718/http-status-assertions
Browse files Browse the repository at this point in the history
Make Rails/HttpStatus aware of Rails specific response assertions
  • Loading branch information
koic committed May 1, 2024
2 parents 28b274b + 571da40 commit 49b5fca
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1267](https://github.com/rubocop/rubocop-rails/pull/1267): Make `Rails/HttpStatus` aware of Rails-specific response assertions. ([@tldn0718][])
14 changes: 12 additions & 2 deletions lib/rubocop/cop/rails/http_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ module Rails
# render plain: 'foo/bar', status: 304
# redirect_to root_url, status: 301
# head 200
# assert_response 200
# assert_redirected_to '/some/path', status: 301
#
# # good
# render :foo, status: :ok
# render json: { foo: 'bar' }, status: :ok
# render plain: 'foo/bar', status: :not_modified
# redirect_to root_url, status: :moved_permanently
# head :ok
# assert_response :ok
# assert_redirected_to '/some/path', status: :moved_permanently
#
# @example EnforcedStyle: numeric
# # bad
Expand All @@ -28,25 +32,31 @@ module Rails
# render plain: 'foo/bar', status: :not_modified
# redirect_to root_url, status: :moved_permanently
# head :ok
# assert_response :ok
# assert_redirected_to '/some/path', status: :moved_permanently
#
# # good
# render :foo, status: 200
# render json: { foo: 'bar' }, status: 404
# render plain: 'foo/bar', status: 304
# redirect_to root_url, status: 301
# head 200
# assert_response 200
# assert_redirected_to '/some/path', status: 301
#
class HttpStatus < Base
include ConfigurableEnforcedStyle
extend AutoCorrector

RESTRICT_ON_SEND = %i[render redirect_to head].freeze
RESTRICT_ON_SEND = %i[render redirect_to head assert_response assert_redirected_to].freeze

def_node_matcher :http_status, <<~PATTERN
{
(send nil? {:render :redirect_to} _ $hash)
(send nil? {:render :redirect_to} $hash)
(send nil? :head ${int sym} ...)
(send nil? {:head :assert_response} ${int sym} ...)
(send nil? :assert_redirected_to _ $hash ...)
(send nil? :assert_redirected_to $hash ...)
}
PATTERN

Expand Down
46 changes: 46 additions & 0 deletions spec/rubocop/cop/rails/http_status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
^^^ Prefer `:ok` over `200` to define HTTP status code.
head 200, location: 'accounts'
^^^ Prefer `:ok` over `200` to define HTTP status code.
assert_response 200
^^^ Prefer `:ok` over `200` to define HTTP status code.
assert_response 404, 'message'
^^^ Prefer `:not_found` over `404` to define HTTP status code.
assert_redirected_to '/some/path', status: 301
^^^ Prefer `:moved_permanently` over `301` to define HTTP status code.
assert_redirected_to action: 'index', status: 301
^^^ Prefer `:moved_permanently` over `301` to define HTTP status code.
assert_redirected_to '/some/path', { status: 301 }, 'message'
^^^ Prefer `:moved_permanently` over `301` to define HTTP status code.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -36,6 +46,11 @@
redirect_to root_path(utm_source: :pr, utm_medium: :web), status: :moved_permanently
head :ok
head :ok, location: 'accounts'
assert_response :ok
assert_response :not_found, 'message'
assert_redirected_to '/some/path', status: :moved_permanently
assert_redirected_to action: 'index', status: :moved_permanently
assert_redirected_to '/some/path', { status: :moved_permanently }, 'message'
RUBY
end

Expand All @@ -58,6 +73,10 @@
redirect_to root_url, status: :moved_permanently
redirect_to root_path(utm_source: :pr, utm_medium: :web), status: :moved_permanently
head :ok
assert_response :ok
assert_response :not_found, 'message'
assert_redirected_to '/some/path', status: :moved_permanently
assert_redirected_to action: 'index', status: :moved_permanently
RUBY
end

Expand All @@ -69,6 +88,10 @@
redirect_to root_url, status: 550
redirect_to root_path(utm_source: :pr, utm_medium: :web), status: 550
head 550
assert_response 550
assert_response 550, 'message'
assert_redirected_to '/some/path', status: 550
assert_redirected_to action: 'index', status: 550
RUBY
end

Expand Down Expand Up @@ -102,6 +125,16 @@
^^^ Prefer `200` over `:ok` to define HTTP status code.
head :ok, location: 'accounts'
^^^ Prefer `200` over `:ok` to define HTTP status code.
assert_response :ok
^^^ Prefer `200` over `:ok` to define HTTP status code.
assert_response :not_found, 'message'
^^^^^^^^^^ Prefer `404` over `:not_found` to define HTTP status code.
assert_redirected_to '/some/path', status: :moved_permanently
^^^^^^^^^^^^^^^^^^ Prefer `301` over `:moved_permanently` to define HTTP status code.
assert_redirected_to action: 'index', status: :moved_permanently
^^^^^^^^^^^^^^^^^^ Prefer `301` over `:moved_permanently` to define HTTP status code.
assert_redirected_to '/some/path', { status: :moved_permanently }, 'message'
^^^^^^^^^^^^^^^^^^ Prefer `301` over `:moved_permanently` to define HTTP status code.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -114,6 +147,11 @@
redirect_to root_path(utm_source: :pr, utm_medium: :web), status: 301
head 200
head 200, location: 'accounts'
assert_response 200
assert_response 404, 'message'
assert_redirected_to '/some/path', status: 301
assert_redirected_to action: 'index', status: 301
assert_redirected_to '/some/path', { status: 301 }, 'message'
RUBY
end

Expand All @@ -125,6 +163,10 @@
redirect_to root_url, status: 301
redirect_to root_path(utm_source: :pr, utm_medium: :web), status: 301
head 200
assert_response 200
assert_response 404, 'message'
assert_redirected_to '/some/path', status: 301
assert_redirected_to action: 'index', status: 301
RUBY
end

Expand All @@ -134,6 +176,10 @@
render :foo, status: :success
render :foo, status: :missing
render :foo, status: :redirect
assert_response :error
assert_response :success
assert_response :missing
assert_response :redirect
RUBY
end

Expand Down

0 comments on commit 49b5fca

Please sign in to comment.