|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::Rails::DeprecatedHttpStatusNames, :config do |
| 4 | + context 'when Rack is older than 3.1' do |
| 5 | + let(:gem_versions) { { 'rack' => '3.0.0' } } |
| 6 | + |
| 7 | + it 'does nothing' do |
| 8 | + expect_no_offenses(<<~RUBY) |
| 9 | + render json: { error: 'Invalid data' }, status: :unprocessable_entity |
| 10 | + head :payload_too_large |
| 11 | + RUBY |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + context 'when Rack is 3.1 or later' do |
| 16 | + let(:gem_versions) { { 'rack' => '3.1.0' } } |
| 17 | + |
| 18 | + context 'with :unprocessable_entity' do |
| 19 | + it 'registers an offense when using :unprocessable_entity in render' do |
| 20 | + expect_offense(<<~RUBY) |
| 21 | + render json: { error: 'Invalid data' }, status: :unprocessable_entity |
| 22 | + ^^^^^^^^^^^^^^^^^^^^^ Use `:unprocessable_content` instead of `:unprocessable_entity`. The `:unprocessable_entity` status is deprecated. |
| 23 | + RUBY |
| 24 | + |
| 25 | + expect_correction(<<~RUBY) |
| 26 | + render json: { error: 'Invalid data' }, status: :unprocessable_content |
| 27 | + RUBY |
| 28 | + end |
| 29 | + |
| 30 | + it 'registers an offense when using :unprocessable_entity in head' do |
| 31 | + expect_offense(<<~RUBY) |
| 32 | + head :unprocessable_entity |
| 33 | + ^^^^^^^^^^^^^^^^^^^^^ Use `:unprocessable_content` instead of `:unprocessable_entity`. The `:unprocessable_entity` status is deprecated. |
| 34 | + RUBY |
| 35 | + |
| 36 | + expect_correction(<<~RUBY) |
| 37 | + head :unprocessable_content |
| 38 | + RUBY |
| 39 | + end |
| 40 | + |
| 41 | + it 'registers an offense when using :unprocessable_entity in redirect_to' do |
| 42 | + expect_offense(<<~RUBY) |
| 43 | + redirect_to some_path, status: :unprocessable_entity |
| 44 | + ^^^^^^^^^^^^^^^^^^^^^ Use `:unprocessable_content` instead of `:unprocessable_entity`. The `:unprocessable_entity` status is deprecated. |
| 45 | + RUBY |
| 46 | + |
| 47 | + expect_correction(<<~RUBY) |
| 48 | + redirect_to some_path, status: :unprocessable_content |
| 49 | + RUBY |
| 50 | + end |
| 51 | + |
| 52 | + it 'registers an offense when using :unprocessable_entity in assert_response' do |
| 53 | + expect_offense(<<~RUBY) |
| 54 | + assert_response :unprocessable_entity |
| 55 | + ^^^^^^^^^^^^^^^^^^^^^ Use `:unprocessable_content` instead of `:unprocessable_entity`. The `:unprocessable_entity` status is deprecated. |
| 56 | + RUBY |
| 57 | + |
| 58 | + expect_correction(<<~RUBY) |
| 59 | + assert_response :unprocessable_content |
| 60 | + RUBY |
| 61 | + end |
| 62 | + |
| 63 | + it 'registers an offense when using :unprocessable_entity in assert_redirected_to' do |
| 64 | + expect_offense(<<~RUBY) |
| 65 | + assert_redirected_to some_path, status: :unprocessable_entity |
| 66 | + ^^^^^^^^^^^^^^^^^^^^^ Use `:unprocessable_content` instead of `:unprocessable_entity`. The `:unprocessable_entity` status is deprecated. |
| 67 | + RUBY |
| 68 | + |
| 69 | + expect_correction(<<~RUBY) |
| 70 | + assert_redirected_to some_path, status: :unprocessable_content |
| 71 | + RUBY |
| 72 | + end |
| 73 | + |
| 74 | + it 'registers an offense when using :unprocessable_entity in ternary expression' do |
| 75 | + expect_offense(<<~RUBY) |
| 76 | + render json: { error: 'Invalid data' }, status: some_condition ? :unprocessable_entity : :ok |
| 77 | + ^^^^^^^^^^^^^^^^^^^^^ Use `:unprocessable_content` instead of `:unprocessable_entity`. The `:unprocessable_entity` status is deprecated. |
| 78 | + RUBY |
| 79 | + |
| 80 | + expect_correction(<<~RUBY) |
| 81 | + render json: { error: 'Invalid data' }, status: some_condition ? :unprocessable_content : :ok |
| 82 | + RUBY |
| 83 | + end |
| 84 | + |
| 85 | + it 'does not register an offense when using :unprocessable_content' do |
| 86 | + expect_no_offenses(<<~RUBY) |
| 87 | + render json: { error: 'Invalid data' }, status: :unprocessable_content |
| 88 | + RUBY |
| 89 | + end |
| 90 | + |
| 91 | + it 'does not register an offense when using :unprocessable_entity in hash key' do |
| 92 | + expect_no_offenses(<<~RUBY) |
| 93 | + { unprocessable_entity: 'Invalid data' } |
| 94 | + RUBY |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + context 'with :payload_too_large' do |
| 99 | + it 'registers an offense when using :payload_too_large in render' do |
| 100 | + expect_offense(<<~RUBY) |
| 101 | + render json: { error: 'File too big' }, status: :payload_too_large |
| 102 | + ^^^^^^^^^^^^^^^^^^ Use `:content_too_large` instead of `:payload_too_large`. The `:payload_too_large` status is deprecated. |
| 103 | + RUBY |
| 104 | + |
| 105 | + expect_correction(<<~RUBY) |
| 106 | + render json: { error: 'File too big' }, status: :content_too_large |
| 107 | + RUBY |
| 108 | + end |
| 109 | + |
| 110 | + it 'registers an offense when using :payload_too_large in head' do |
| 111 | + expect_offense(<<~RUBY) |
| 112 | + head :payload_too_large |
| 113 | + ^^^^^^^^^^^^^^^^^^ Use `:content_too_large` instead of `:payload_too_large`. The `:payload_too_large` status is deprecated. |
| 114 | + RUBY |
| 115 | + |
| 116 | + expect_correction(<<~RUBY) |
| 117 | + head :content_too_large |
| 118 | + RUBY |
| 119 | + end |
| 120 | + |
| 121 | + it 'registers an offense when using :payload_too_large in redirect_to' do |
| 122 | + expect_offense(<<~RUBY) |
| 123 | + redirect_to some_path, status: :payload_too_large |
| 124 | + ^^^^^^^^^^^^^^^^^^ Use `:content_too_large` instead of `:payload_too_large`. The `:payload_too_large` status is deprecated. |
| 125 | + RUBY |
| 126 | + |
| 127 | + expect_correction(<<~RUBY) |
| 128 | + redirect_to some_path, status: :content_too_large |
| 129 | + RUBY |
| 130 | + end |
| 131 | + |
| 132 | + it 'registers an offense when using :payload_too_large in assert_response' do |
| 133 | + expect_offense(<<~RUBY) |
| 134 | + assert_response :payload_too_large |
| 135 | + ^^^^^^^^^^^^^^^^^^ Use `:content_too_large` instead of `:payload_too_large`. The `:payload_too_large` status is deprecated. |
| 136 | + RUBY |
| 137 | + |
| 138 | + expect_correction(<<~RUBY) |
| 139 | + assert_response :content_too_large |
| 140 | + RUBY |
| 141 | + end |
| 142 | + |
| 143 | + it 'registers an offense when using :payload_too_large in assert_redirected_to' do |
| 144 | + expect_offense(<<~RUBY) |
| 145 | + assert_redirected_to some_path, status: :payload_too_large |
| 146 | + ^^^^^^^^^^^^^^^^^^ Use `:content_too_large` instead of `:payload_too_large`. The `:payload_too_large` status is deprecated. |
| 147 | + RUBY |
| 148 | + |
| 149 | + expect_correction(<<~RUBY) |
| 150 | + assert_redirected_to some_path, status: :content_too_large |
| 151 | + RUBY |
| 152 | + end |
| 153 | + |
| 154 | + it 'registers an offense when using :payload_too_large in ternary expression' do |
| 155 | + expect_offense(<<~RUBY) |
| 156 | + render json: { error: 'File too big' }, status: some_condition ? :payload_too_large : :ok |
| 157 | + ^^^^^^^^^^^^^^^^^^ Use `:content_too_large` instead of `:payload_too_large`. The `:payload_too_large` status is deprecated. |
| 158 | + RUBY |
| 159 | + |
| 160 | + expect_correction(<<~RUBY) |
| 161 | + render json: { error: 'File too big' }, status: some_condition ? :content_too_large : :ok |
| 162 | + RUBY |
| 163 | + end |
| 164 | + |
| 165 | + it 'does not register an offense when using :content_too_large' do |
| 166 | + expect_no_offenses(<<~RUBY) |
| 167 | + render json: { error: 'File too big' }, status: :content_too_large |
| 168 | + RUBY |
| 169 | + end |
| 170 | + |
| 171 | + it 'does not register an offense when using :payload_too_large in hash key' do |
| 172 | + expect_no_offenses(<<~RUBY) |
| 173 | + { payload_too_large: 'File too big' } |
| 174 | + RUBY |
| 175 | + end |
| 176 | + end |
| 177 | + |
| 178 | + context 'with mixed deprecated statuses' do |
| 179 | + it 'handles multiple deprecated statuses in the same code' do |
| 180 | + expect_offense(<<~RUBY) |
| 181 | + head :unprocessable_entity |
| 182 | + ^^^^^^^^^^^^^^^^^^^^^ Use `:unprocessable_content` instead of `:unprocessable_entity`. The `:unprocessable_entity` status is deprecated. |
| 183 | + head :payload_too_large |
| 184 | + ^^^^^^^^^^^^^^^^^^ Use `:content_too_large` instead of `:payload_too_large`. The `:payload_too_large` status is deprecated. |
| 185 | + RUBY |
| 186 | + |
| 187 | + expect_correction(<<~RUBY) |
| 188 | + head :unprocessable_content |
| 189 | + head :content_too_large |
| 190 | + RUBY |
| 191 | + end |
| 192 | + end |
| 193 | + end |
| 194 | +end |
0 commit comments