Skip to content

Commit 9bb089a

Browse files
authored
Merge pull request #1951 from wbreeze/successful
Prevent "have_http_status" from using deprecated methods in Rails 5.2+
2 parents f5a7b40 + 811e390 commit 9bb089a

File tree

4 files changed

+241
-151
lines changed

4 files changed

+241
-151
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ matrix:
135135
env: RAILS_VERSION=3-2-stable
136136
- rvm: 2.4.2
137137
env: RAILS_VERSION='~> 3.1.12'
138+
- rvm: 2.4.2
139+
env: RAILS_VERSION=5.2.0.rc1
138140
allow_failures:
139141
- rvm: 2.2.2
140142
env: RAILS_VERSION=master

Changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Enhancements:
88

99
Bug Fixes:
1010

11-
* Escape quotation characters when producing method names for system spec
11+
* Escape quotation characters when producing method names for system spec
1212
screenshots. (Shane Cavanaugh, #1955)
1313

1414
### 3.7.2 / 2017-11-20

lib/rspec/rails/matchers/have_http_status.rb

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,11 @@ class GenericStatus < RSpec::Matchers::BuiltIn::BaseMatcher
243243
# code "group"
244244
# @see https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/testing/test_response.rb `ActionDispatch::TestResponse`
245245
def self.valid_statuses
246-
[:error, :success, :missing, :redirect]
246+
[
247+
:error, :success, :missing,
248+
:server_error, :successful, :not_found,
249+
:redirect
250+
]
247251
end
248252

249253
def initialize(type)
@@ -256,11 +260,11 @@ def initialize(type)
256260
end
257261

258262
# @return [Boolean] `true` if Rack's associated numeric HTTP code matched
259-
# the `response` code
263+
# the `response` code or the named response status
260264
def matches?(response)
261265
test_response = as_test_response(response)
262266
@actual = test_response.response_code
263-
test_response.send("#{expected}?")
267+
check_expected_status(test_response, expected)
264268
rescue TypeError => _ignored
265269
@invalid_response = response
266270
false
@@ -283,6 +287,28 @@ def failure_message_when_negated
283287
"expected the response not to have #{type_message} but it was #{actual}"
284288
end
285289

290+
protected
291+
292+
if 5 < ::Rails::VERSION::MAJOR ||
293+
(::Rails::VERSION::MAJOR == 5 && 2 <= ::Rails::VERSION::MINOR)
294+
RESPONSE_METHODS = {
295+
:success => 'successful',
296+
:error => 'server_error',
297+
:missing => 'not_found'
298+
}.freeze
299+
else
300+
RESPONSE_METHODS = {
301+
:successful => 'success',
302+
:server_error => 'error',
303+
:not_found => 'missing'
304+
}.freeze
305+
end
306+
307+
def check_expected_status(test_response, expected)
308+
test_response.send(
309+
"#{RESPONSE_METHODS.fetch(expected, expected)}?")
310+
end
311+
286312
private
287313

288314
# @return [String] formating the expected status and associated code(s)
@@ -316,11 +342,11 @@ def type_codes
316342
# @see https://github.com/rails/rails/blob/ca200378/actionpack/lib/action_dispatch/http/response.rb#L74
317343
# @see https://github.com/rack/rack/blob/ce4a3959/lib/rack/response.rb#L119-L122
318344
@type_codes ||= case expected
319-
when :error
345+
when :error, :server_error
320346
"5xx"
321-
when :success
347+
when :success, :successful
322348
"2xx"
323-
when :missing
349+
when :missing, :not_found
324350
"404"
325351
when :redirect
326352
"3xx"

0 commit comments

Comments
 (0)