You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rack::Test is a small, simple testing API for Rack apps. It can be used on its
own or as a reusable starting point for Web frameworks and testing libraries
to build on.
Features
Allows for submitting requests and testing responses
Maintains a cookie jar across requests
Supports request headers used for subsequent requests
Follow redirects when requested
Examples
These examples use test/unit but it's equally possible to use rack-test with
other testing frameworks such as minitest or rspec.
require"test/unit"require"rack/test"require"json"classHomepageTest < Test::Unit::TestCaseincludeRack::Test::Methodsdefapplambda{ |env| [200,{'content-type'=>'text/plain'},['All responses are OK']]}enddeftest_response_is_ok# Optionally set headers used for all requests in this spec:#header 'accept-charset', 'utf-8'# First argument is treated as the pathget'/'assertlast_response.ok?assert_equal'All responses are OK',last_response.bodyenddefdelete_with_url_params_and_body# First argument can have a query string## Second argument is used as the parameters for the request, which will be# included in the request body for non-GET requests.delete'/?foo=bar',JSON.generate('baz'=>'zot')enddefpost_with_json# Third argument is the rack environment to use for the request. The following# entries in the submitted rack environment are treated specially (in addition# to options supported by `Rack::MockRequest#env_for`:## :cookie : Set a cookie for the current session before submitting the request.## :query_params : Set parameters for the query string (as opposed to the body).# Value should be a hash of parameters.## :xhr : Set HTTP_X_REQUESTED_WITH env key to XMLHttpRequest.post(uri,JSON.generate('baz'=>'zot'),'CONTENT_TYPE'=>'application/json')endend
rack-test will test the app returned by the app method. If you are loading middleware
in a config.ru file, and want to test that, you should load the Rack app created from
the config.ru file:
If your application does not automatically use the Rack::Lint middleware in test mode,
and you want to test that requests to and responses from your application are compliant
with the Rack specification, you should manually use the Rack::Lint middleware: