Module: Minitest::Assertions

Defined in:
lib/minitest/rack.rb,
lib/minitest/rack/json.rb,
lib/minitest/rack/status.rb,
lib/minitest/rack/headers.rb

Overview

add support for Assert syntax

Defined Under Namespace

Modules: Expectations

Instance Method Summary collapse

Instance Method Details

#assert_acceptedBoolean

Tests that the HTTP response status is 202 Accepted. The request has been accepted but has not been processed yet.

NOTE! This code does not guarantee that the request will process successfully.

Examples:

assert_accepted # passes if response status is 202 Accepted

Returns:

  • (Boolean)

    true if the response status is 202 Accepted

See Also:



67
68
69
# File 'lib/minitest/rack/status.rb', line 67

def assert_accepted
  assert_status 202
end

#assert_bad_gatewayBoolean

Tests that the HTTP response status is 502 Bad Gateway. The server is acting as a proxy and did not receive an acceptable response from the upstream server. This indicates that the proxy server was unable to complete the request due to issues with the upstream server.

Examples:

assert_bad_gateway # passes if response status is 502 Bad Gateway

Returns:

  • (Boolean)

    true if the response status is 502 Bad Gateway

See Also:



463
464
465
# File 'lib/minitest/rack/status.rb', line 463

def assert_bad_gateway
  assert_status 502
end

#assert_bad_requestBoolean

Tests that the HTTP response status is 400 Bad Request. The request could not be fulfilled due to the incorrect syntax of the request. This indicates that the request was malformed, contains invalid syntax, or cannot be processed by the server.

Examples:

assert_bad_request # passes if response status is 400 Bad Request

Returns:

  • (Boolean)

    true if the response status is 400 Bad Request

See Also:



259
260
261
# File 'lib/minitest/rack/status.rb', line 259

def assert_bad_request
  assert_status 400
end

#assert_body(res) ⇒ Boolean

Assert that the response body matches the expected value

Parameters:

  • res (String)

    The expected response body

Returns:

  • (Boolean)

    true if the assertion passes

Raises:

  • (Minitest::Assertion)

    if the response body doesn’t match



18
19
20
21
22
# File 'lib/minitest/rack.rb', line 18

def assert_body(res)
  msg = "Expected response to be '#{res}', but was '#{last_response.body}'"

  assert_equal(last_response.body, res, msg)
end

#assert_createdBoolean

Tests that the HTTP response status is 201 Created.

Examples:

assert_created # passes if response status is 201 Created

Returns:

  • (Boolean)

    true if the response status is 201 Created

See Also:



51
52
53
# File 'lib/minitest/rack/status.rb', line 51

def assert_created
  assert_status 201
end

#assert_forbiddenBoolean

Tests that the HTTP response status is 403 Forbidden. The request was formatted correctly but the server is refusing to supply the requested resource. Unlike 401, authenticating will not make a difference in the server’s response.

Examples:

assert_forbidden # passes if response status is 403 Forbidden

Returns:

  • (Boolean)

    true if the response status is 403 Forbidden

See Also:



291
292
293
# File 'lib/minitest/rack/status.rb', line 291

def assert_forbidden
  assert_status 403
end

#assert_foundBoolean

Tests that the HTTP response status is 302 Found. The HTTP 1.0 specification described this status as “Moved Temporarily”, but popular browsers respond to this status similar to behavior intended for 303. The resource can be retrieved by referencing the returned URI.

Examples:

assert_found # passes if response status is 302 Found

Returns:

  • (Boolean)

    true if the response status is 302 Found

See Also:



162
163
164
# File 'lib/minitest/rack/status.rb', line 162

def assert_found
  assert_status 302
end

#assert_header(header, contents) ⇒ Boolean

Test if a specific response header has an expected value Essentially, a shortcut for testing the ‘last_response.header` value

Example:

assert_header('Accept', 'text/plain')

Parameters:

  • header (String)

    The name of the HTTP header to check

  • contents (String)

    The expected value of the header

Returns:

  • (Boolean)

    True if header matches expected value



21
22
23
24
25
26
# File 'lib/minitest/rack/headers.rb', line 21

def assert_header(header, contents)
  msg = "Expected response header '#{header}' to be '#{contents}', "
  msg << "but was '#{last_response.headers[header]}'"

  assert_equal(contents, last_response.headers[header], msg)
end

#assert_header_accept(type) ⇒ Boolean

Tests that a header contains the Accept content-type Accept indicates which content-types the client can process

Example:

assert_header_accept("text/plain")

Parameters:

  • type (String)

    The content-type to check for

Returns:

  • (Boolean)

    True if Accept header matches the specified type



38
39
40
# File 'lib/minitest/rack/headers.rb', line 38

def assert_header_accept(type)
  assert_header('Accept', type)
end

#assert_header_application_type(type) ⇒ Boolean

Test for the ‘application/` type header via `Content-Type` Valid application types include pdf, json, xml, etc.

Example:

assert_header_application_type('pdf')
# Tests Content-Type equals 'application/pdf'

Parameters:

  • type (String)

    The application content-type suffix (e.g. “pdf”, “json”)

Returns:

  • (Boolean)

    True if Content-Type header matches “application/type”



53
54
55
# File 'lib/minitest/rack/headers.rb', line 53

def assert_header_application_type(type)
  assert_header('Content-Type', "application/#{type}")
end

#assert_header_attachment(filename) ⇒ Boolean

Tests that the Content-Disposition header indicates an attachment download Content-Disposition suggests whether content should be displayed inline or downloaded as an attachment with an optional filename

Raise a “File Download” dialogue box for a known MIME type with binary format or suggest a filename for dynamic content. Quotes are necessary with special characters

Example:

assert_header_attachment('document.pdf')
# Tests Content-Disposition equals 'attachment; filename="document.pdf"'

Parameters:

  • filename (String)

    The suggested filename for the attachment

Returns:

  • (Boolean)

    True if Content-Disposition matches expected attachment format



251
252
253
# File 'lib/minitest/rack/headers.rb', line 251

def assert_header_attachment(filename)
  assert_header('Content-Disposition', "attachment; filename=\"#{filename}\"")
end

#assert_header_content_encoding(encoding_str) ⇒ Boolean Also known as: assert_header_encoding

Test if Content-Encoding header matches expected value Content-Encoding specifies what encodings have been applied to the payload. Common encoding types include gzip, compress, deflate, br

Example:

assert_header_content_encoding('gzip')

assert_header_encoding('gzip')

Parameters:

  • encoding_str (String)

    The expected content encoding value

Returns:

  • (Boolean)

    True if Content-Encoding matches expected value



70
71
72
# File 'lib/minitest/rack/headers.rb', line 70

def assert_header_content_encoding(encoding_str)
  assert_header('Content-Encoding', encoding_str)
end

#assert_header_content_language(lang) ⇒ Boolean Also known as: assert_header_language

Tests that the Content-Language header matches an expected value Content-Language indicates the language of the content returned by the server

Example:

assert_header_content_language('en')
assert_header_content_language('fr')

Parameters:

  • lang (String)

    The expected language value

Returns:

  • (Boolean)

    True if Content-Language matches the expected value



86
87
88
# File 'lib/minitest/rack/headers.rb', line 86

def assert_header_content_language(lang)
  assert_header('Content-Language', lang)
end

#assert_header_content_length(length) ⇒ Boolean

Tests that the Content-Length header matches the expected value Content-Length specifies the size of the response body in bytes

Example:

assert_header_content_length(348)
assert_header_content_length("348")

Parameters:

  • length (String, Integer)

    The expected content length value

Returns:

  • (Boolean)

    True if Content-Length matches the expected value



102
103
104
# File 'lib/minitest/rack/headers.rb', line 102

def assert_header_content_length(length)
  assert_header('Content-Length', length.to_s)
end

#assert_header_content_location(url) ⇒ Boolean

Test if Content-Location header matches expected value Content-Location indicates an alternate location for the returned data

Example:

assert_header_content_location('/index.htm')

Parameters:

  • url (String)

    The expected alternate URL value

Returns:

  • (Boolean)

    True if Content-Location matches expected value



116
117
118
# File 'lib/minitest/rack/headers.rb', line 116

def assert_header_content_location(url)
  assert_header('Content-Location', url.to_s)
end

#assert_header_content_type(type) ⇒ Boolean

Tests that the Content-Type header matches an expected value Content-Type specifies the MIME type of the content being sent

Example:

assert_header_content_type('text/html; charset=utf-8')

Parameters:

  • type (String)

    The expected MIME type

Returns:

  • (Boolean)

    True if Content-Type matches expected value



130
131
132
# File 'lib/minitest/rack/headers.rb', line 130

def assert_header_content_type(type)
  assert_header('Content-Type', type)
end

#assert_header_etag(tag) ⇒ Boolean

Tests that the ETag header matches an expected value ETag is a unique identifier for a specific version of a resource, often a hash

Example:

assert_header_etag('"737060cd8c284d8af7ad3082f209582d"')

Parameters:

  • tag (String)

    The expected ETag value

Returns:

  • (Boolean)

    True if ETag matches expected value



144
145
146
# File 'lib/minitest/rack/headers.rb', line 144

def assert_header_etag(tag)
  assert_header('ETag', tag)
end

#assert_header_expires(timestamp) ⇒ Boolean

Tests that the Expires header matches an expected timestamp Expires defines when the response should be considered stale

Example:

assert_header_expires('Thu, 01 Dec 1994 16:00:00 GMT')

Parameters:

  • timestamp (String)

    The expected expiration date in HTTP-date format

Returns:

  • (Boolean)

    True if Expires matches expected timestamp



158
159
160
# File 'lib/minitest/rack/headers.rb', line 158

def assert_header_expires(timestamp)
  assert_header('Expires', timestamp)
end

#assert_header_image_type(type) ⇒ Boolean

Tests that the Content-Type header matches an image MIME type Validates that content is an image with the specified format

Example:

assert_header_image_type('png')
# Tests Content-Type equals 'image/png'

Parameters:

  • type (String, Symbol)

    The expected image format (bmp, gif, jpg, jpeg, png, tiff)

Returns:

  • (Boolean)

    True if Content-Type matches “image/type”



173
174
175
# File 'lib/minitest/rack/headers.rb', line 173

def assert_header_image_type(type)
  assert_header('Content-Type', "image/#{type}")
end

#assert_header_last_modified(timestamp) ⇒ Boolean

Tests that the Last-Modified header matches an expected timestamp Last-Modified indicates when the resource was last changed

Example:

assert_header_last_modified('Tue, 15 Nov 1994 12:45:26 GMT')

Parameters:

  • timestamp (String)

    The expected last modified date in HTTP-date format

Returns:

  • (Boolean)

    True if Last-Modified matches expected timestamp



187
188
189
# File 'lib/minitest/rack/headers.rb', line 187

def assert_header_last_modified(timestamp)
  assert_header('Last-Modified', timestamp)
end

#assert_header_server(server_str) ⇒ Boolean

Tests that the Server header matches an expected value Server specifies information about the software used by the origin server

Example:

assert_header_server('Apache/2.4.1 (Unix)')

Parameters:

  • server_str (String)

    The expected server identification string

Returns:

  • (Boolean)

    True if Server header matches expected value



201
202
203
# File 'lib/minitest/rack/headers.rb', line 201

def assert_header_server(server_str)
  assert_header('Server', server_str)
end

#assert_header_text_type(type) ⇒ Boolean

Test for the ‘text/` type header via `Content-Type` Tests that the Content-Type header matches a text/* MIME type

Example:

assert_header_text_type('plain')
# Tests Content-Type equals 'text/plain'

Parameters:

  • type (String)

    The text content-type suffix (e.g. “plain”, “html”)

Returns:

  • (Boolean)

    True if Content-Type header matches “text/type”



216
217
218
# File 'lib/minitest/rack/headers.rb', line 216

def assert_header_text_type(type)
  assert_header('Content-Type', "text/#{type}")
end

#assert_header_type_is_jsonBoolean

Tests that the Content-Type header is set to “application/json” Validates that the response is formatted as JSON data

Example:

assert_header_type_is_json
# Tests Content-Type equals 'application/json'

Returns:

  • (Boolean)

    True if Content-Type matches “application/json”



264
265
266
# File 'lib/minitest/rack/headers.rb', line 264

def assert_header_type_is_json
  assert_header('Content-Type', 'application/json')
end

#assert_header_www_authenticate(auth_str) ⇒ Boolean

Tests that the WWW-Authenticate header matches an expected value WWW-Authenticate indicates the authentication scheme that should be used to access the requested resource

Example:

assert_header_www_authenticate('Basic')
assert_header_www_authenticate('Bearer realm="example"')

Parameters:

  • auth_str (String)

    The expected authentication scheme value

Returns:

  • (Boolean)

    True if WWW-Authenticate matches expected value



232
233
234
# File 'lib/minitest/rack/headers.rb', line 232

def assert_header_www_authenticate(auth_str)
  assert_header('WWW-Authenticate', auth_str)
end

#assert_internal_server_errorBoolean

Tests that the HTTP response status is 500 Internal Server Error. A generic status for an error in the server itself. This indicates that the server encountered an unexpected condition that prevented it from fulfilling the request.

Examples:

assert_internal_server_error # passes if response status is 500 Internal Server Error

Returns:

  • (Boolean)

    true if the response status is 500 Internal Server Error

See Also:



432
433
434
# File 'lib/minitest/rack/status.rb', line 432

def assert_internal_server_error
  assert_status 500
end

#assert_json_data(res) ⇒ Boolean

Asserts against the presence of specific key/value pairs in the response JSON data. When testing endpoint responses for JSON data conformity, it ensures the response matches the expected data exactly. Takes a hash of expected data and validates it against the parsed JSON response.

present in the JSON response data

Examples:

# Response body contains: {"id": 1, "name": "test"}
assert_json_data({id: 1, name: "test"}) # => true

Parameters:

  • res (Hash)

    hash containing the expected key/value pairs that should be

Returns:

  • (Boolean)

    true when response data matches expected data



36
37
38
39
40
41
42
# File 'lib/minitest/rack/json.rb', line 36

def assert_json_data(res)
  data = json_data

  msg = "Expected response JSON data to be '#{res}', but was '#{data}'"

  assert_equal(res, data, msg)
end

#assert_json_error(errno = '404') ⇒ Boolean

Asserts that the response JSON data contains an ‘error’ attribute with the specified error code and validates that its value matches the expected error code string. Default error code is “404” if none specified.

Parameters:

  • errno (String) (defaults to: '404')

    the expected error code value (defaults to “404”)

Returns:

  • (Boolean)

    true when the error value matches the expected error code



69
70
71
72
73
74
75
76
# File 'lib/minitest/rack/json.rb', line 69

def assert_json_error(errno = '404')
  data = json_data

  msg = "Expected response JSON data to include '\"error\": #{errno}', "
  msg << "but was '#{data.inspect}'"

  assert_equal(errno.to_s, data['error'].to_s, msg)
end

#assert_json_key(key) ⇒ Boolean

Verifies that the response JSON data contains the specified key with a non-empty value. This method checks for the presence of a given key in the response and validates that its value is not empty, ensuring the expected data field exists and has content.

Parameters:

  • key (String, Symbol)

    the key to check for in the response

Returns:

  • (Boolean)

    true when the key exists and has a non-empty value



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/minitest/rack/json.rb', line 137

def assert_json_key(key)
  data = json_data
  key = key.to_s

  msg = 'Expected response JSON data to include '
  msg << "key: '#{key}', but JSON is '#{data}'"

  # handle the model being present
  if data.key?(key)
    refute_empty(data[key], msg)
  else
    assert_has_key data, key, msg
  end
end

#assert_json_message(message) ⇒ Boolean

Verifies that the response JSON data contains a message attribute with the specified message string. This method checks for the presence of a “message” key and validates that its value matches the expected message text.

Parameters:

  • msg (String)

    the expected message value to check for in the response

Returns:

  • (Boolean)

    true when the message value matches the expected message string



87
88
89
90
91
92
93
94
# File 'lib/minitest/rack/json.rb', line 87

def assert_json_message(message)
  data = json_data

  msg = "Expected response JSON data to include '\"message\": #{message}', "
  msg << "but was '#{data.inspect}'"

  assert_equal(message, data['message'], msg)
end

#assert_json_model(key, model) ⇒ Boolean

Verifies that the response JSON data contains a specific model attribute with the expected model data. This method checks if the response contains a key matching the provided model name and validates that its JSON representation matches the expected model object.

Examples:

# Response body contains: {"user": {"id": 1, "name": "Bob"}}
user = User.new(id: 1, name: "Bob")
assert_json_model('user', user) # => true

Parameters:

  • key (String, Symbol)

    the model key to check for in the response

  • model (Object)

    the model object whose JSON representation matches the response data

Returns:

  • (Boolean)

    true when the model JSON matches the response data for the given key



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/minitest/rack/json.rb', line 111

def assert_json_model(key, model)
  data = json_data
  key = key.to_s

  msg = 'Expected response JSON data to include '

  # handle wrong key value being passed
  if data.key?(key)
    msg << "'#{key}: #{model.to_json}', but was '#{data[key].to_json}'"

    assert_equal(model.values.to_json, data[key].to_json, msg)
  else
    msg << "key: '#{key}', but JSON is: '#{data}'"

    assert_has_key data, key, msg
  end
end

#assert_json_model_key(model, key) ⇒ Boolean

Verifies that the response JSON data contains a nested key within a model attribute and that the key’s value is not empty. This method checks if the response contains a model key and a nested key within it, validating that the nested key’s value exists and is not empty.

rubocop:disable Metrics/MethodLength

Parameters:

  • model (String, Symbol)

    the model key to check for in the response

  • key (String, Symbol)

    the nested key to check within the model object

Returns:

  • (Boolean)

    true when the nested key exists and has a non-empty value



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/minitest/rack/json.rb', line 163

def assert_json_model_key(model, key)
  data = json_data
  model = model.to_s
  key = key.to_s

  msg = 'Expected response JSON data to include '

  # handle the model being present
  if data.key?(model)
    if data[model].key?(key)
      msg = 'life is great'

      refute_empty(data[model][key], msg)
    else
      msg << "model.key: '#{model}.#{key}', but it did not"

      assert_has_key data, "#{model}.#{key}", msg
    end
  else
    msg << "model: '#{model}', but it did not"

    assert_has_key data, model, msg
  end
end

#assert_json_success(bool: true) ⇒ Boolean

Verify if the response JSON data contains a success attribute with specified truth value. This method specifically checks for the presence of a “success” key and validates its value against the expected boolean. By default, it expects true unless otherwise specified.

Parameters:

  • bool (Boolean) (defaults to: true)

    the expected value of success attribute (defaults to true)

Returns:

  • (Boolean)

    true when the success value matches the expected boolean



52
53
54
55
56
57
58
59
# File 'lib/minitest/rack/json.rb', line 52

def assert_json_success(bool: true)
  data = json_data

  msg = "Expected response JSON data to include '\"success\": #{bool}', "
  msg << "but was '#{data.inspect}'"

  assert_equal(bool, data['success'], msg)
end

#assert_loop_detectedBoolean

Tests that the HTTP response status is 508 Loop Detected. The server detected an infinite loop while processing the request. This error occurs when the server detects that the client’s request would result in an infinite loop of operations, typically in WebDAV scenarios.

Examples:

assert_loop_detected # passes if response status is 508 Loop Detected

Returns:

  • (Boolean)

    true if the response status is 508 Loop Detected

See Also:



495
496
497
# File 'lib/minitest/rack/status.rb', line 495

def assert_loop_detected
  assert_status 508
end

#assert_method_not_allowedBoolean

Tests that the HTTP response status is 405 Method Not Allowed. The resource was requested using a method that is not allowed. For example, requesting a resource via a POST method when the resource only supports GET.

Examples:

assert_method_not_allowed # passes if response status is 405 Method Not Allowed

Returns:

  • (Boolean)

    true if the response status is 405 Method Not Allowed

See Also:



321
322
323
# File 'lib/minitest/rack/status.rb', line 321

def assert_method_not_allowed
  assert_status 405
end

#assert_moved_permanentlyBoolean

Tests that the HTTP response status is 301 Moved Permanently. The resource has been moved and all further requests should reference its new URI.

Examples:

assert_moved_permanently # passes if response status is 301 Moved Permanently

Returns:

  • (Boolean)

    true if the response status is 301 Moved Permanently

See Also:



146
147
148
# File 'lib/minitest/rack/status.rb', line 146

def assert_moved_permanently
  assert_status 301
end

#assert_multiple_choicesBoolean

Tests that the HTTP response status is 300 Multiple Choices. Indicates multiple options for the user to follow. This can happen when the server has several suitable responses and the client must select one themselves.

Examples:

assert_multiple_choices # passes if response status is 300 Multiple Choices

Returns:

  • (Boolean)

    true if the response status is 300 Multiple Choices

See Also:



132
133
134
# File 'lib/minitest/rack/status.rb', line 132

def assert_multiple_choices
  assert_status 300
end

#assert_no_contentBoolean

Tests that the HTTP response status is 204 No Content. The server accepted the request but is not returning any content. This is often used as a response to a DELETE request.

Examples:

assert_no_content # passes if response status is 204 No Content

Returns:

  • (Boolean)

    true if the response status is 204 No Content

See Also:



82
83
84
# File 'lib/minitest/rack/status.rb', line 82

def assert_no_content
  assert_status 204
end

#assert_not_acceptableBoolean

Tests that the HTTP response status is 406 Not Acceptable. The resource is valid, but cannot be provided in a format specified in the Accept headers in the request.

The server sends this error when the client requests a format that the server does not support.

Examples:

assert_not_acceptable # passes if response status is 406 Not Acceptable

Returns:

  • (Boolean)

    true if the response status is 406 Not Acceptable

See Also:



339
340
341
# File 'lib/minitest/rack/status.rb', line 339

def assert_not_acceptable
  assert_status 406
end

#assert_not_foundBoolean

Tests that the HTTP response status is 404 Not Found. The resource could not be found. This is often used as a catch-all for all invalid URIs requested of the server.

Examples:

assert_not_found # passes if response status is 404 Not Found

Returns:

  • (Boolean)

    true if the response status is 404 Not Found

See Also:



306
307
308
# File 'lib/minitest/rack/status.rb', line 306

def assert_not_found
  assert_status 404
end

#assert_not_implementedBoolean

Tests that the HTTP response status is 501 Not Implemented. The server cannot respond to the request. This usually implies that the server could possibly support the request in the future — otherwise a 4xx status may be more appropriate.

Examples:

assert_not_implemented # passes if response status is 501 Not Implemented

Returns:

  • (Boolean)

    true if the response status is 501 Not Implemented

See Also:



447
448
449
# File 'lib/minitest/rack/status.rb', line 447

def assert_not_implemented
  assert_status 501
end

#assert_not_modifiedBoolean

Tests that the HTTP response status is 304 Not Modified. The resource has not been modified since the version specified in If-Modified-Since or If-Match headers. The resource will not be returned in response body.

Examples:

assert_not_modified # passes if response status is 304 Not Modified

Returns:

  • (Boolean)

    true if the response status is 304 Not Modified

See Also:



178
179
180
# File 'lib/minitest/rack/status.rb', line 178

def assert_not_modified
  assert_status 304
end

#assert_okBoolean

Tests that the HTTP response status is 200 OK.

Examples:

assert_ok # passes if response status is 200 OK

Returns:

  • (Boolean)

    true if the response status is 200 OK

See Also:



38
39
40
# File 'lib/minitest/rack/status.rb', line 38

def assert_ok
  assert_status 200
end

#assert_partial_contentBoolean

The server is delivering only a portion of the content, as requested by the client via a range header.

Examples:

assert_partial_content # passes if response status is 206 Partial Content

Returns:

  • (Boolean)

    true if the response status is 206 Partial Content

See Also:



115
116
117
# File 'lib/minitest/rack/status.rb', line 115

def assert_partial_content
  assert_status 206
end

#assert_permanent_redirectBoolean

Tests that the HTTP response status is 308 Permanent Redirect. Experimental. The request and all future requests should be repeated with the URI provided in the response. The HTTP method is not allowed to be changed in the subsequent request.

Examples:

assert_permanent_redirect # passes if response status is 308 Permanent Redirect

Returns:

  • (Boolean)

    true if the response status is 308 Permanent Redirect

See Also:



241
242
243
# File 'lib/minitest/rack/status.rb', line 241

def assert_permanent_redirect
  assert_status 308
end

#assert_proxy_authentication_requiredBoolean

Tests that the HTTP response status is 407 Proxy Authentication Required. Authentication is required with the proxy before requests can be fulfilled. The proxy server must return information about the authentication scheme required in a Proxy-Authenticate header.

Examples:

assert_proxy_authentication_required # passes if response status is 407

Returns:

  • (Boolean)

    true if the response status is 407 Proxy Authentication Required

See Also:



355
356
357
# File 'lib/minitest/rack/status.rb', line 355

def assert_proxy_authentication_required
  assert_status 407
end

#assert_request_timeoutBoolean

Tests that the HTTP response status is 408 Request Timeout. The server timed out waiting for a request from the client. The client is allowed to repeat the request.

Examples:

assert_request_timeout # passes if response status is 408 Request Timeout

Returns:

  • (Boolean)

    true if the response status is 408 Request Timeout

See Also:



370
371
372
# File 'lib/minitest/rack/status.rb', line 370

def assert_request_timeout
  assert_status 408
end

#assert_reset_contentBoolean

Tests that the HTTP response status is 205 Reset Content.

The server accepted the request but requires the client to reset the document view. Similar to a 204 No Content response but this response requires the requester to reset the document view.

Examples:

assert_reset_content # passes if response status is 205 Reset Content

Returns:

  • (Boolean)

    true if the response status is 205 Reset Content

See Also:



99
100
101
# File 'lib/minitest/rack/status.rb', line 99

def assert_reset_content
  assert_status 205
end

#assert_service_unavailableBoolean

Tests that the HTTP response status is 503 Service Unavailable. The server is currently unavailable (because it is overloaded or down for maintenance). Generally, this is a temporary state and the server should return information about when to retry with a Retry-After header.

Examples:

assert_service_unavailable # passes if response status is 503 Service Unavailable

Returns:

  • (Boolean)

    true if the response status is 503 Service Unavailable

See Also:



479
480
481
# File 'lib/minitest/rack/status.rb', line 479

def assert_service_unavailable
  assert_status 503
end

#assert_status(status) ⇒ Boolean

Tests that the HTTP response status matches the expected value.

Examples:

assert_status(200) # passes if response status is 200 OK
assert_status(404) # passes if response status is 404 Not Found

Parameters:

  • status (Integer)

    the expected HTTP status code

Returns:

  • (Boolean)

    true if the status matches, false otherwise



21
22
23
24
25
# File 'lib/minitest/rack/status.rb', line 21

def assert_status(status)
  msg = "Expected response status to be '#{status}', but was '#{last_response.status}'"

  assert_equal(status, last_response.status, msg)
end

#assert_switch_proxyBoolean

Deprecated.

No longer used in HTTP 1.1

Tests that the HTTP response status is 306 Switch Proxy. This status code is no longer used. Originally meant to indicate that subsequent requests should use the specified proxy.

NOTE: This code is deprecated in HTTP 1.1 and should not be used.

Examples:

assert_switch_proxy # passes if response status is 306 Switch Proxy

Returns:

  • (Boolean)

    true if the response status is 306 Switch Proxy



210
211
212
# File 'lib/minitest/rack/status.rb', line 210

def assert_switch_proxy
  assert_status 306
end

#assert_temporary_redirectBoolean

Tests that the HTTP response status is 307 Temporary Redirect. HTTP 1.1. The request should be repeated with the URI provided in the response, but future requests should still call the original URI.

Examples:

assert_temporary_redirect # passes if response status is 307 Temporary Redirect

Returns:

  • (Boolean)

    true if the response status is 307 Temporary Redirect

See Also:



225
226
227
# File 'lib/minitest/rack/status.rb', line 225

def assert_temporary_redirect
  assert_status 307
end

#assert_too_many_requestsBoolean

Tests that the HTTP response status is 429 Too Many Requests. The user has sent too many requests in a given amount of time (“rate limiting”). This status indicates that the client should retry after some time.

Examples:

assert_too_many_requests # passes if response status is 429 Too Many Requests

Returns:

  • (Boolean)

    true if the response status is 429 Too Many Requests

See Also:



415
416
417
# File 'lib/minitest/rack/status.rb', line 415

def assert_too_many_requests
  assert_status 429
end

#assert_unauthorizedBoolean

Tests that the HTTP response status is 401 Unauthorized. The requester is not authorized to access the resource. This status code is used when authentication is required but has either failed or not been provided. Similar to 403 Forbidden, but indicates specifically that authentication is possible.

Examples:

assert_unauthorized # passes if response status is 401 Unauthorized

Returns:

  • (Boolean)

    true if the response status is 401 Unauthorized

See Also:



275
276
277
# File 'lib/minitest/rack/status.rb', line 275

def assert_unauthorized
  assert_status 401
end

#assert_unprocessable_entityBoolean

Tests that the HTTP response status is 422 Unprocessable Entity. The request was well-formed but was unable to be processed due to semantic errors. Commonly used when validation fails or malformed content is submitted.

Examples:

assert_unprocessable_entity # passes if response status is 422 Unprocessable Entity

Returns:

  • (Boolean)

    true if the response status is 422 Unprocessable Entity

See Also:



400
401
402
# File 'lib/minitest/rack/status.rb', line 400

def assert_unprocessable_entity
  assert_status 422
end

#assert_unsupported_media_typeBoolean

Tests that the HTTP response status is 415 Unsupported Media Type. The client provided data with a media type that the server does not support. For example, submitting JSON data when only XML is supported.

Examples:

assert_unsupported_media_type # passes if response status is 415 Unsupported Media Type

Returns:

  • (Boolean)

    true if the response status is 415 Unsupported Media Type

See Also:



385
386
387
# File 'lib/minitest/rack/status.rb', line 385

def assert_unsupported_media_type
  assert_status 415
end

#assert_use_proxyBoolean

Tests that the HTTP response status is 305 Use Proxy. Indicates that the requested resource can only be accessed through a proxy specified in the response’s Location header.

Examples:

assert_use_proxy # passes if response status is 305 Use Proxy

Returns:

  • (Boolean)

    true if the response status is 305 Use Proxy

See Also:



193
194
195
# File 'lib/minitest/rack/status.rb', line 193

def assert_use_proxy
  assert_status 305
end

#delete_json(path, params = {}, headers = {}) ⇒ Object

Shortcut for sending DELETE requests as JSON

delete_json("/api/users/1234")


217
218
219
# File 'lib/minitest/rack/json.rb', line 217

def delete_json(path, params = {}, headers = {})
  json_request(:delete, path, params, headers)
end

#get_json(path, params = {}, headers = {}) ⇒ Object

Shortcut for sending GET requests as JSON

get_json("/api/users")


193
194
195
# File 'lib/minitest/rack/json.rb', line 193

def get_json(path, params = {}, headers = {})
  json_request(:get, path, params, headers)
end

#json_dataHash

Parse the response body of the last response as JSON using the native Ruby JSON parser. This method helps in quickly grabbing the JSON data from the response to verify in assertions.

Returns:

  • (Hash)

    parsed JSON data from the response body



18
19
20
# File 'lib/minitest/rack/json.rb', line 18

def json_data
  ::JSON.parse(last_response.body)
end

#post_json(path, params = {}, headers = {}) ⇒ Object

Shortcut for sending POST requests as JSON

post_json("/api/users", {name: "Joe"})


201
202
203
# File 'lib/minitest/rack/json.rb', line 201

def post_json(path, params = {}, headers = {})
  json_request(:post, path, params, headers)
end

#put_json(path, params = {}, headers = {}) ⇒ Object

Shortcut for sending PUT requests as JSON

put_json("/api/users/1234", {id: 1, name: "Joe"})


209
210
211
# File 'lib/minitest/rack/json.rb', line 209

def put_json(path, params = {}, headers = {})
  json_request(:put, path, params, headers)
end