API for Testing Rack Apps with easy
$ [sudo] gem install sonar
include Sonar in your tests.
Or use it directly, by initialize a session via
When mixin used, call
app RackApp inside testing suite to set app to be tested.
require 'sonar' class MyTests < MiniTest::Unit::TestCase include Sonar def setup app MyRackApp end def test get '/url' assert_equal last_response.status, 200 end end
Spec.new do app MyRackApp get '/url' expect(last_response.status) == 200 end
Multiple apps can be tested within same suite.
Each app will run own session.
require 'sonar' class MyTests < MiniTest::Unit::TestCase include Sonar def setup app MyRackApp end def test # querying default app get '/url' assert_equal last_response.status, 200 # testing ForumApp app ForumApp get '/posts' assert_equal last_response.status, 200 # back to default app app MyRackApp get '/url' assert_equal last_response.status, 200 end end
When using session manually, you should set app at initialization.
session = SonarSession.new MyRackApp session.get '/url' assert_equal session.last_response.status, 200
Sometimes you need to start over with a new app in pristine state, i.e. no cookies, no headers etc.
To achieve this, simply call
This will reset currently tested app. Other tested apps will stay untouched.
When creating sessions manually, app can NOT be switched/reset.
To test another app, simply create another session.
Use one of
to make requests via Sonar browser.
To make a secure request, add
s_get '/path' s_post '/path' # etc.
To make a request via XHR, aka Ajax, add
get_x '/path' post_x '/path' # etc.
To make a secure request via XHR, add both
s_get_x '/path' s_post_x '/path' # etc.
In terms of arguments, making HTTP requests via Sonar is identical to calling regular Ruby methods.
That's it, you do not need to join parameters into a string.
Just pass them as usual arguments:
post '/news', :create, :title => rand post '/news', :update, id, :title => rand get '/news', :delete, id
Previous example works just fine, however it is redundant and inconsistent.
Just imagine that tested app changed its base URL from /news to /headlines.
The solution is simple.
map to define a base URL that will be prepended to each request,
except ones starting with a slash or a protocol(http://, https:// etc.) of course.
Spec.new do app MyRackApp map '/news' post :create, :title => rand post :update, id, :title => rand get :delete, id end
Note: requests starting with a slash or protocol(http://, https:// etc.)
wont use base URL defined by
Note: when you switching tested app, make sure you also change the map.
To disable mapping, simply call
['name'] = 'value'
Delete a cookie:
Clear all cookies:
Each app uses its own cookies jar.
Sonar allow to set headers that will be sent to app on all consequent requests.
header['User-Agent'] = 'Sonar' header['Content-Type'] = 'text/plain' header['rack.input'] = 'someString' # etc.
header = headers['User-Agent'] # etc.
Delete a header:
Clear all headers:
Each app uses its own headers.
Reset earlier set Basic authorization header:
Reset earlier set Digest authorization header:
Reset ANY earlier set authorization header:
By default, Sonar wont follow redirects.
If last response is a redirect and you want Sonar to follow it, use