The key here is recognising app
is an instance of ActionDispatch::Integration::Session
, which includes ActionDispatch::TestProcess
and therefore has a #session
method that'll supply the authenticity token, once you've woken it up with a request.
We can then use this via the app::post
helper, itself a simple wrapper for app
's own #process
method, which documents the calling parameters.
Putting all of those pieces together, in Rails 5 and later, we might write:
app.get ''
token = app.session[:_csrf_token]
app.post '/servers/important_method', params: { authenticity_token: token }
Note for older versions of Rails:
Since we're talking to the integration test API here, you may use similar forms in integration tests. When upgrading older Rails apps (v4 and earlier), you may find the following test code failing:
post '/servers/important_method', authenticity_token: token
and this should be revised to the new syntax, wrapping the parameters in params: { ... }
.