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
-
#assert_accepted ⇒ Boolean
Tests that the HTTP response status is 202 Accepted.
-
#assert_bad_gateway ⇒ Boolean
Tests that the HTTP response status is 502 Bad Gateway.
-
#assert_bad_request ⇒ Boolean
Tests that the HTTP response status is 400 Bad Request.
-
#assert_body(res) ⇒ Boolean
Assert that the response body matches the expected value.
-
#assert_created ⇒ Boolean
Tests that the HTTP response status is 201 Created.
-
#assert_forbidden ⇒ Boolean
Tests that the HTTP response status is 403 Forbidden.
-
#assert_found ⇒ Boolean
Tests that the HTTP response status is 302 Found.
-
#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.
-
#assert_header_accept(type) ⇒ Boolean
Tests that a header contains the Accept content-type Accept indicates which content-types the client can process.
-
#assert_header_application_type(type) ⇒ Boolean
Test for the ‘application/` type header via `Content-Type` Valid application types include pdf, json, xml, etc.
-
#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.
-
#assert_header_content_encoding(encoding_str) ⇒ Boolean
(also: #assert_header_encoding)
Test if Content-Encoding header matches expected value Content-Encoding specifies what encodings have been applied to the payload.
-
#assert_header_content_language(lang) ⇒ Boolean
(also: #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.
-
#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.
-
#assert_header_content_location(url) ⇒ Boolean
Test if Content-Location header matches expected value Content-Location indicates an alternate location for the returned data.
-
#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.
-
#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.
-
#assert_header_expires(timestamp) ⇒ Boolean
Tests that the Expires header matches an expected timestamp Expires defines when the response should be considered stale.
-
#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.
-
#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.
-
#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.
-
#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.
-
#assert_header_type_is_json ⇒ Boolean
Tests that the Content-Type header is set to “application/json” Validates that the response is formatted as JSON data.
-
#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.
-
#assert_internal_server_error ⇒ Boolean
Tests that the HTTP response status is 500 Internal Server Error.
-
#assert_json_data(res) ⇒ Boolean
Asserts against the presence of specific key/value pairs in the response JSON data.
-
#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.
-
#assert_json_key(key) ⇒ Boolean
Verifies that the response JSON data contains the specified key with a non-empty value.
-
#assert_json_message(message) ⇒ Boolean
Verifies that the response JSON data contains a message attribute with the specified message string.
-
#assert_json_model(key, model) ⇒ Boolean
Verifies that the response JSON data contains a specific model attribute with the expected model data.
-
#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.
-
#assert_json_success(bool: true) ⇒ Boolean
Verify if the response JSON data contains a success attribute with specified truth value.
-
#assert_loop_detected ⇒ Boolean
Tests that the HTTP response status is 508 Loop Detected.
-
#assert_method_not_allowed ⇒ Boolean
Tests that the HTTP response status is 405 Method Not Allowed.
-
#assert_moved_permanently ⇒ Boolean
Tests that the HTTP response status is 301 Moved Permanently.
-
#assert_multiple_choices ⇒ Boolean
Tests that the HTTP response status is 300 Multiple Choices.
-
#assert_no_content ⇒ Boolean
Tests that the HTTP response status is 204 No Content.
-
#assert_not_acceptable ⇒ Boolean
Tests that the HTTP response status is 406 Not Acceptable.
-
#assert_not_found ⇒ Boolean
Tests that the HTTP response status is 404 Not Found.
-
#assert_not_implemented ⇒ Boolean
Tests that the HTTP response status is 501 Not Implemented.
-
#assert_not_modified ⇒ Boolean
Tests that the HTTP response status is 304 Not Modified.
-
#assert_ok ⇒ Boolean
Tests that the HTTP response status is 200 OK.
-
#assert_partial_content ⇒ Boolean
The server is delivering only a portion of the content, as requested by the client via a range header.
-
#assert_permanent_redirect ⇒ Boolean
Tests that the HTTP response status is 308 Permanent Redirect.
-
#assert_proxy_authentication_required ⇒ Boolean
Tests that the HTTP response status is 407 Proxy Authentication Required.
-
#assert_request_timeout ⇒ Boolean
Tests that the HTTP response status is 408 Request Timeout.
-
#assert_reset_content ⇒ Boolean
Tests that the HTTP response status is 205 Reset Content.
-
#assert_service_unavailable ⇒ Boolean
Tests that the HTTP response status is 503 Service Unavailable.
-
#assert_status(status) ⇒ Boolean
Tests that the HTTP response status matches the expected value.
-
#assert_switch_proxy ⇒ Boolean
deprecated
Deprecated.
No longer used in HTTP 1.1
-
#assert_temporary_redirect ⇒ Boolean
Tests that the HTTP response status is 307 Temporary Redirect.
-
#assert_too_many_requests ⇒ Boolean
Tests that the HTTP response status is 429 Too Many Requests.
-
#assert_unauthorized ⇒ Boolean
Tests that the HTTP response status is 401 Unauthorized.
-
#assert_unprocessable_entity ⇒ Boolean
Tests that the HTTP response status is 422 Unprocessable Entity.
-
#assert_unsupported_media_type ⇒ Boolean
Tests that the HTTP response status is 415 Unsupported Media Type.
-
#assert_use_proxy ⇒ Boolean
Tests that the HTTP response status is 305 Use Proxy.
-
#delete_json(path, params = {}, headers = {}) ⇒ Object
Shortcut for sending DELETE requests as JSON.
-
#get_json(path, params = {}, headers = {}) ⇒ Object
Shortcut for sending GET requests as JSON.
-
#json_data ⇒ Hash
Parse the response body of the last response as JSON using the native Ruby JSON parser.
-
#post_json(path, params = {}, headers = {}) ⇒ Object
Shortcut for sending POST requests as JSON.
-
#put_json(path, params = {}, headers = {}) ⇒ Object
Shortcut for sending PUT requests as JSON.
Instance Method Details
#assert_accepted ⇒ Boolean
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.
67 68 69 |
# File 'lib/minitest/rack/status.rb', line 67 def assert_accepted assert_status 202 end |
#assert_bad_gateway ⇒ Boolean
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.
463 464 465 |
# File 'lib/minitest/rack/status.rb', line 463 def assert_bad_gateway assert_status 502 end |
#assert_bad_request ⇒ Boolean
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.
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
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_created ⇒ Boolean
Tests that the HTTP response status is 201 Created.
51 52 53 |
# File 'lib/minitest/rack/status.rb', line 51 def assert_created assert_status 201 end |
#assert_forbidden ⇒ Boolean
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.
291 292 293 |
# File 'lib/minitest/rack/status.rb', line 291 def assert_forbidden assert_status 403 end |
#assert_found ⇒ Boolean
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.
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')
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")
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'
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:
('document.pdf')
# Tests Content-Disposition equals 'attachment; filename="document.pdf"'
251 252 253 |
# File 'lib/minitest/rack/headers.rb', line 251 def (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')
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')
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")
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')
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')
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"')
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')
158 159 160 |
# File 'lib/minitest/rack/headers.rb', line 158 def assert_header_expires() assert_header('Expires', ) 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'
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')
187 188 189 |
# File 'lib/minitest/rack/headers.rb', line 187 def assert_header_last_modified() assert_header('Last-Modified', ) 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)')
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'
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_json ⇒ Boolean
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'
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"')
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_error ⇒ Boolean
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.
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
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.
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.
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.
87 88 89 90 91 92 93 94 |
# File 'lib/minitest/rack/json.rb', line 87 def () data = json_data msg = "Expected response JSON data to include '\"message\": #{}', " msg << "but was '#{data.inspect}'" assert_equal(, 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.
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
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.
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_detected ⇒ Boolean
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.
495 496 497 |
# File 'lib/minitest/rack/status.rb', line 495 def assert_loop_detected assert_status 508 end |
#assert_method_not_allowed ⇒ Boolean
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.
321 322 323 |
# File 'lib/minitest/rack/status.rb', line 321 def assert_method_not_allowed assert_status 405 end |
#assert_moved_permanently ⇒ Boolean
Tests that the HTTP response status is 301 Moved Permanently. The resource has been moved and all further requests should reference its new URI.
146 147 148 |
# File 'lib/minitest/rack/status.rb', line 146 def assert_moved_permanently assert_status 301 end |
#assert_multiple_choices ⇒ Boolean
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.
132 133 134 |
# File 'lib/minitest/rack/status.rb', line 132 def assert_multiple_choices assert_status 300 end |
#assert_no_content ⇒ Boolean
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.
82 83 84 |
# File 'lib/minitest/rack/status.rb', line 82 def assert_no_content assert_status 204 end |
#assert_not_acceptable ⇒ Boolean
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.
339 340 341 |
# File 'lib/minitest/rack/status.rb', line 339 def assert_not_acceptable assert_status 406 end |
#assert_not_found ⇒ Boolean
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.
306 307 308 |
# File 'lib/minitest/rack/status.rb', line 306 def assert_not_found assert_status 404 end |
#assert_not_implemented ⇒ Boolean
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.
447 448 449 |
# File 'lib/minitest/rack/status.rb', line 447 def assert_not_implemented assert_status 501 end |
#assert_not_modified ⇒ Boolean
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.
178 179 180 |
# File 'lib/minitest/rack/status.rb', line 178 def assert_not_modified assert_status 304 end |
#assert_ok ⇒ Boolean
Tests that the HTTP response status is 200 OK.
38 39 40 |
# File 'lib/minitest/rack/status.rb', line 38 def assert_ok assert_status 200 end |
#assert_partial_content ⇒ Boolean
The server is delivering only a portion of the content, as requested by the client via a range header.
115 116 117 |
# File 'lib/minitest/rack/status.rb', line 115 def assert_partial_content assert_status 206 end |
#assert_permanent_redirect ⇒ Boolean
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.
241 242 243 |
# File 'lib/minitest/rack/status.rb', line 241 def assert_permanent_redirect assert_status 308 end |
#assert_proxy_authentication_required ⇒ Boolean
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.
355 356 357 |
# File 'lib/minitest/rack/status.rb', line 355 def assert_proxy_authentication_required assert_status 407 end |
#assert_request_timeout ⇒ Boolean
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.
370 371 372 |
# File 'lib/minitest/rack/status.rb', line 370 def assert_request_timeout assert_status 408 end |
#assert_reset_content ⇒ Boolean
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.
99 100 101 |
# File 'lib/minitest/rack/status.rb', line 99 def assert_reset_content assert_status 205 end |
#assert_service_unavailable ⇒ Boolean
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.
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.
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_proxy ⇒ Boolean
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.
210 211 212 |
# File 'lib/minitest/rack/status.rb', line 210 def assert_switch_proxy assert_status 306 end |
#assert_temporary_redirect ⇒ Boolean
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.
225 226 227 |
# File 'lib/minitest/rack/status.rb', line 225 def assert_temporary_redirect assert_status 307 end |
#assert_too_many_requests ⇒ Boolean
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.
415 416 417 |
# File 'lib/minitest/rack/status.rb', line 415 def assert_too_many_requests assert_status 429 end |
#assert_unauthorized ⇒ Boolean
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.
275 276 277 |
# File 'lib/minitest/rack/status.rb', line 275 def assert_status 401 end |
#assert_unprocessable_entity ⇒ Boolean
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.
400 401 402 |
# File 'lib/minitest/rack/status.rb', line 400 def assert_unprocessable_entity assert_status 422 end |
#assert_unsupported_media_type ⇒ Boolean
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.
385 386 387 |
# File 'lib/minitest/rack/status.rb', line 385 def assert_unsupported_media_type assert_status 415 end |
#assert_use_proxy ⇒ Boolean
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.
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_data ⇒ Hash
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.
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 |