-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathhave_http_status.rb
385 lines (344 loc) · 14.3 KB
/
have_http_status.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# The following code inspired and modified from Rails' `assert_response`:
#
# https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/testing/assertions/response.rb#L22-L38
#
# Thank you to all the Rails devs who did the heavy lifting on this!
module RSpec
module Rails
module Matchers
# Namespace for various implementations of `have_http_status`.
#
# @api private
module HaveHttpStatus
# Instantiates an instance of the proper matcher based on the provided
# `target`.
#
# @param target [Object] expected http status or code
# @return response matcher instance
def self.matcher_for_status(target)
if GenericStatus.valid_statuses.include?(target)
GenericStatus.new(target)
elsif Symbol === target
SymbolicStatus.new(target)
else
NumericCode.new(target)
end
end
# @api private
# Conversion function to coerce the provided object into an
# `ActionDispatch::TestResponse`.
#
# @param obj [Object] object to convert to a response
# @return [ActionDispatch::TestResponse]
def as_test_response(obj)
if ::ActionDispatch::Response === obj
::ActionDispatch::TestResponse.from_response(obj)
elsif ::ActionDispatch::TestResponse === obj
obj
elsif obj.respond_to?(:status_code) && obj.respond_to?(:response_headers)
# Acts As Capybara Session
# Hack to support `Capybara::Session` without having to load
# Capybara or catch `NameError`s for the undefined constants
obj = ActionDispatch::Response.new.tap do |resp|
resp.status = obj.status_code
resp.headers.clear
resp.headers.merge!(obj.response_headers)
resp.body = obj.body
resp.request = ActionDispatch::Request.new({})
end
::ActionDispatch::TestResponse.from_response(obj)
else
raise TypeError, "Invalid response type: #{obj}"
end
end
module_function :as_test_response
# @return [String, nil] a formatted failure message if
# `@invalid_response` is present, `nil` otherwise
def invalid_response_type_message
return unless @invalid_response
"expected a response object, but an instance of " \
"#{@invalid_response.class} was received"
end
# @api private
# Provides an implementation for `have_http_status` matching against
# numeric http status codes.
#
# Not intended to be instantiated directly.
#
# @example
# expect(response).to have_http_status(404)
#
# @see RSpec::Rails::Matchers#have_http_status
class NumericCode < RSpec::Rails::Matchers::BaseMatcher
include HaveHttpStatus
def initialize(code)
@expected = code.to_i
@actual = nil
@invalid_response = nil
end
# @param [Object] response object providing an http code to match
# @return [Boolean] `true` if the numeric code matched the `response` code
def matches?(response)
test_response = as_test_response(response)
@actual = test_response.response_code.to_i
expected == @actual
rescue TypeError => _ignored
@invalid_response = response
false
end
# @return [String]
def description
"respond with numeric status code #{expected}"
end
# @return [String] explaining why the match failed
def failure_message
invalid_response_type_message ||
"expected the response to have status code #{expected.inspect}" \
" but it was #{actual.inspect}"
end
# @return [String] explaining why the match failed
def failure_message_when_negated
invalid_response_type_message ||
"expected the response not to have status code " \
"#{expected.inspect} but it did"
end
end
# @api private
# Provides an implementation for `have_http_status` matching against
# Rack symbol http status codes.
#
# Not intended to be instantiated directly.
#
# @example
# expect(response).to have_http_status(:created)
#
# @see RSpec::Rails::Matchers#have_http_status
# @see https://github.com/rack/rack/blob/master/lib/rack/utils.rb `Rack::Utils::SYMBOL_TO_STATUS_CODE`
class SymbolicStatus < RSpec::Rails::Matchers::BaseMatcher
include HaveHttpStatus
def initialize(status)
@expected_status = status
@actual = nil
@invalid_response = nil
set_expected_code!
end
# @param [Object] response object providing an http code to match
# @return [Boolean] `true` if Rack's associated numeric HTTP code matched
# the `response` code
def matches?(response)
test_response = as_test_response(response)
@actual = test_response.response_code
expected == @actual
rescue TypeError => _ignored
@invalid_response = response
false
end
# @return [String]
def description
"respond with status code #{pp_expected}"
end
# @return [String] explaining why the match failed
def failure_message
invalid_response_type_message ||
"expected the response to have status code #{pp_expected} but it" \
" was #{pp_actual}"
end
# @return [String] explaining why the match failed
def failure_message_when_negated
invalid_response_type_message ||
"expected the response not to have status code #{pp_expected} " \
"but it did"
end
# The initialized expected status symbol
attr_reader :expected_status
private :expected_status
private
# @return [Symbol] representing the actual http numeric code
def actual_status
return unless actual
@actual_status ||= compute_status_from(actual)
end
# Reverse lookup of the Rack status code symbol based on the numeric
# http code
#
# @param code [Fixnum] http status code to look up
# @return [Symbol] representing the http numeric code
def compute_status_from(code)
status, _ = Rack::Utils::SYMBOL_TO_STATUS_CODE.find do |_, c|
c == code
end
status
end
# @return [String] pretty format the actual response status
def pp_actual
pp_status(actual_status, actual)
end
# @return [String] pretty format the expected status and associated code
def pp_expected
pp_status(expected_status, expected)
end
# @return [String] pretty format the actual response status
def pp_status(status, code)
if status
"#{status.inspect} (#{code})"
else
code.to_s
end
end
# Sets `expected` to the numeric http code based on the Rack
# `expected_status` status
#
# @see Rack::Utils::SYMBOL_TO_STATUS_CODE
# @raise [ArgumentError] if an associated code could not be found
def set_expected_code!
@expected ||=
Rack::Utils::SYMBOL_TO_STATUS_CODE.fetch(expected_status) do
raise ArgumentError,
"Invalid HTTP status: #{expected_status.inspect}"
end
end
end
# @api private
# Provides an implementation for `have_http_status` matching against
# `ActionDispatch::TestResponse` http status category queries.
#
# Not intended to be instantiated directly.
#
# @example
# expect(response).to have_http_status(:success)
# expect(response).to have_http_status(:error)
# expect(response).to have_http_status(:missing)
# expect(response).to have_http_status(:redirect)
#
# @see RSpec::Rails::Matchers#have_http_status
# @see https://github.com/rails/rails/blob/6-0-stable/actionpack/lib/action_dispatch/testing/test_response.rb `ActionDispatch::TestResponse`
class GenericStatus < RSpec::Rails::Matchers::BaseMatcher
include HaveHttpStatus
# @return [Array<Symbol>] of status codes which represent a HTTP status
# code "group"
# @see https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/testing/test_response.rb `ActionDispatch::TestResponse`
def self.valid_statuses
[
:error, :success, :missing,
:server_error, :successful, :not_found,
:redirect
]
end
def initialize(type)
unless self.class.valid_statuses.include?(type)
raise ArgumentError, "Invalid generic HTTP status: #{type.inspect}"
end
@expected = type
@actual = nil
@invalid_response = nil
end
# @return [Boolean] `true` if Rack's associated numeric HTTP code matched
# the `response` code or the named response status
def matches?(response)
test_response = as_test_response(response)
@actual = test_response.response_code
check_expected_status(test_response, expected)
rescue TypeError => _ignored
@invalid_response = response
false
end
# @return [String]
def description
"respond with #{type_message}"
end
# @return [String] explaining why the match failed
def failure_message
invalid_response_type_message ||
"expected the response to have #{type_message} but it was #{actual}"
end
# @return [String] explaining why the match failed
def failure_message_when_negated
invalid_response_type_message ||
"expected the response not to have #{type_message} but it was #{actual}"
end
protected
RESPONSE_METHODS = {
success: 'successful',
error: 'server_error',
missing: 'not_found'
}.freeze
def check_expected_status(test_response, expected)
test_response.send(
"#{RESPONSE_METHODS.fetch(expected, expected)}?")
end
private
# @return [String] formating the expected status and associated code(s)
def type_message
@type_message ||= (expected == :error ? "an error" : "a #{expected}") +
" status code (#{type_codes})"
end
# @return [String] formatting the associated code(s) for the various
# status code "groups"
# @see https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/testing/test_response.rb `ActionDispatch::TestResponse`
# @see https://github.com/rack/rack/blob/master/lib/rack/response.rb `Rack::Response`
def type_codes
# At the time of this commit the most recent version of
# `ActionDispatch::TestResponse` defines the following aliases:
#
# alias_method :success?, :successful?
# alias_method :missing?, :not_found?
# alias_method :redirect?, :redirection?
# alias_method :error?, :server_error?
#
# It's parent `ActionDispatch::Response` includes
# `Rack::Response::Helpers` which defines the aliased methods as:
#
# def successful?; status >= 200 && status < 300; end
# def redirection?; status >= 300 && status < 400; end
# def server_error?; status >= 500 && status < 600; end
# def not_found?; status == 404; end
#
# @see https://github.com/rails/rails/blob/ca200378/actionpack/lib/action_dispatch/testing/test_response.rb#L17-L27
# @see https://github.com/rails/rails/blob/ca200378/actionpack/lib/action_dispatch/http/response.rb#L74
# @see https://github.com/rack/rack/blob/ce4a3959/lib/rack/response.rb#L119-L122
@type_codes ||= case expected
when :error, :server_error
"5xx"
when :success, :successful
"2xx"
when :missing, :not_found
"404"
when :redirect
"3xx"
end
end
end
end
# @api public
# Passes if `response` has a matching HTTP status code.
#
# The following symbolic status codes are allowed:
#
# - `Rack::Utils::SYMBOL_TO_STATUS_CODE`
# - One of the defined `ActionDispatch::TestResponse` aliases:
# - `:error`
# - `:missing`
# - `:redirect`
# - `:success`
#
# @example Accepts numeric and symbol statuses
# expect(response).to have_http_status(404)
# expect(response).to have_http_status(:created)
# expect(response).to have_http_status(:success)
# expect(response).to have_http_status(:error)
# expect(response).to have_http_status(:missing)
# expect(response).to have_http_status(:redirect)
#
# @example Works with standard `response` objects and Capybara's `page`
# expect(response).to have_http_status(404)
# expect(page).to have_http_status(:created)
#
# @see https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/testing/test_response.rb `ActionDispatch::TestResponse`
# @see https://github.com/rack/rack/blob/master/lib/rack/utils.rb `Rack::Utils::SYMBOL_TO_STATUS_CODE`
def have_http_status(target)
raise ArgumentError, "Invalid HTTP status: nil" unless target
HaveHttpStatus.matcher_for_status(target)
end
end
end
end