Skip to content

Commit

Permalink
Merge pull request #823 from anthony-robin/http-status-routes
Browse files Browse the repository at this point in the history
[Fix #822] Extends `Rails/HttpStatus` cop to check `routes.rb`
  • Loading branch information
koic authored Oct 22, 2022
2 parents 6051ed8 + 1dcbda1 commit e40d8b9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#822](https://github.com/rubocop/rubocop-rails/issues/822): Extends `Rails/HttpStatus` cop to check `routes.rb`. ([@anthony-robin][])
7 changes: 6 additions & 1 deletion lib/rubocop/cop/rails/http_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ module Rails
# render plain: 'foo/bar', status: 304
# redirect_to root_url, status: 301
# head 200
# get '/foobar', to: redirect('/foobar/baz', 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
# get '/foobar', to: redirect('/foobar/baz', status: :moved_permanently)
#
# @example EnforcedStyle: numeric
# # bad
Expand All @@ -27,25 +29,28 @@ module Rails
# render plain: 'foo/bar', status: :not_modified
# redirect_to root_url, status: :moved_permanently
# head :ok
# get '/foobar', to: redirect('/foobar/baz', 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
# get '/foobar', to: redirect('/foobar/baz', 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 redirect].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? :redirect _ $hash)
}
PATTERN

Expand Down
9 changes: 9 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,8 @@
^^^ Prefer `:ok` over `200` to define HTTP status code.
head 200, location: 'accounts'
^^^ Prefer `:ok` over `200` to define HTTP status code.
get '/foobar', to: redirect('/foobar/baz', status: 301)
^^^ Prefer `:moved_permanently` over `301` to define HTTP status code.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -36,6 +38,7 @@
redirect_to root_path(utm_source: :pr, utm_medium: :web), status: :moved_permanently
head :ok
head :ok, location: 'accounts'
get '/foobar', to: redirect('/foobar/baz', status: :moved_permanently)
RUBY
end

Expand All @@ -47,6 +50,7 @@
redirect_to root_url, status: :moved_permanently
redirect_to root_path(utm_source: :pr, utm_medium: :web), status: :moved_permanently
head :ok
get '/foobar', to: redirect('/foobar/baz', status: :moved_permanently)
RUBY
end

Expand All @@ -58,6 +62,7 @@
redirect_to root_url, status: 550
redirect_to root_path(utm_source: :pr, utm_medium: :web), status: 550
head 550
get '/foobar', to: redirect('/foobar/baz', status: 550)
RUBY
end
end
Expand Down Expand Up @@ -85,6 +90,8 @@
^^^ Prefer `200` over `:ok` to define HTTP status code.
head :ok, location: 'accounts'
^^^ Prefer `200` over `:ok` to define HTTP status code.
get '/foobar', to: redirect('/foobar/baz', status: :moved_permanently)
^^^^^^^^^^^^^^^^^^ Prefer `301` over `:moved_permanently` to define HTTP status code.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -97,6 +104,7 @@
redirect_to root_path(utm_source: :pr, utm_medium: :web), status: 301
head 200
head 200, location: 'accounts'
get '/foobar', to: redirect('/foobar/baz', status: 301)
RUBY
end

Expand All @@ -108,6 +116,7 @@
redirect_to root_url, status: 301
redirect_to root_path(utm_source: :pr, utm_medium: :web), status: 301
head 200
get '/foobar', to: redirect('/foobar/baz', status: 301)
RUBY
end

Expand Down

0 comments on commit e40d8b9

Please sign in to comment.