Class: ActionDispatch::IntegrationTest
- Inherits:
-
ActiveSupport::TestCase
- Object
- Cybertrain::Test::Base
- ActiveSupport::TestCase
- ActionDispatch::IntegrationTest
- Defined in:
- cybertrain/test/integration.rb
Overview
Rails’ base class for request tests: get, post, patch, put,
delete and head against the booted application, in process (no
socket), and assertions on the last response.
Each request answers the Cybertrain::Response (Rails answers the
status) and becomes #response. A path or a URL on the application’s
host (what the *_url helpers write) is requested; a URL on another
host is refused. Forgery protection is off in cybertrain test unless
config/app.rb sets csrf (as in Rails’ test environment); a test turns
it on for some requests with #with_forgery_protection.
Instance Method Summary collapse
-
#assert_redirected_to(path) ⇒ nil
Passes when the last response is a 3xx whose Location is exactly
path. -
#assert_response(expected) ⇒ nil
Passes when the last response’s status is
expected: an Integer, or a Symbol (:success2xx,:redirect3xx,:not_found, …). -
#cookies ⇒ Cybertrain::Test::ClientCookies
This test’s cookie jar: name (a String or a Symbol:
cookies[:user]) to raw (still percent-encoded) value, from every Set-Cookie so far in this test; a cookie past its Max-Age or Expires under the clock (aftertravel_to) is gone, as in Rails. -
#delete(path, params: {}, headers: {}) ⇒ Cybertrain::Response
DELETE;
params:go in the body as a form. -
#fixture_file_upload(path, mime_type = "") ⇒ Cybertrain::Test::Upload
A file to send in
params:of post, patch or put, as Rails’fixture_file_uploadgives one: the request then goes as multipart/form-data, the file under its bracketed key (params: { post: { cover: fixture_file_upload("cover.png", "image/png") } }is the partpost[cover]) and the other params as text parts. -
#follow_redirect! ⇒ Cybertrain::Response
GETs the last response’s Location (a URL on the application’s host by its path and query), without its fragment (
#comments), which a browser never sends. -
#get(path, params: {}, headers: {}) ⇒ Cybertrain::Response
GET;
params:go in the query string. -
#head(path, params: {}, headers: {}) ⇒ Cybertrain::Response
HEAD: the GET route answers and the body is dropped, as the server drops it;
params:go in the query string. -
#initialize ⇒ IntegrationTest
constructor
A new client on Cybertrain::Test.app: an empty cookie jar.
-
#patch(path, params: {}, headers: {}) ⇒ Cybertrain::Response
PATCH;
params:go in the body as a form. -
#post(path, params: {}, headers: {}) ⇒ Cybertrain::Response
POST;
params:go in the body as a form. -
#put(path, params: {}, headers: {}) ⇒ Cybertrain::Response
PUT;
params:go in the body as a form. -
#response ⇒ Cybertrain::Response
The last response (an empty 200 before the first request).
-
#with_forgery_protection { ... } ⇒ nil
Runs the block with forgery protection on, as Rails’
ActionController::Base.allow_forgery_protection = truedoes: its non-GET requests must carry the token from a page the test rendered (else 403).
Methods inherited from ActiveSupport::TestCase
Methods inherited from Cybertrain::Test::Base
Constructor Details
#initialize ⇒ IntegrationTest
A new client on Cybertrain::Test.app: an empty cookie jar.
245 246 247 248 249 250 251 252 |
# File 'cybertrain/test/integration.rb', line 245 def initialize app = Cybertrain::Test.app if app.nil? = Cybertrain::Test::NO_APP raise end @integration_client = Cybertrain::Test::Client.new(app) end |
Instance Method Details
#assert_redirected_to(path) ⇒ nil
Passes when the last response is a 3xx whose Location is exactly path.
379 380 381 |
# File 'cybertrain/test/integration.rb', line 379 def assert_redirected_to(path) Cybertrain::Test.check_redirect(@integration_client.response, path) end |
#assert_response(expected) ⇒ nil
Passes when the last response’s status is expected: an Integer, or
a Symbol (:success 2xx, :redirect 3xx, :not_found, …).
371 372 373 |
# File 'cybertrain/test/integration.rb', line 371 def assert_response(expected) Cybertrain::Test.check_response(@integration_client.response, expected) end |
#cookies ⇒ Cybertrain::Test::ClientCookies
This test’s cookie jar: name (a String or a Symbol: cookies[:user])
to raw (still percent-encoded) value, from every Set-Cookie so far in
this test; a cookie past its Max-Age or Expires under the clock (after
travel_to) is gone, as in Rails.
362 363 364 |
# File 'cybertrain/test/integration.rb', line 362 def @integration_client. end |
#delete(path, params: {}, headers: {}) ⇒ Cybertrain::Response
DELETE; params: go in the body as a form.
300 301 302 |
# File 'cybertrain/test/integration.rb', line 300 def delete(path, params: {}, headers: {}) integration_response(@integration_client.delete(integration_target(path), Cybertrain::Test.flatten_params(params), header_names(headers))) end |
#fixture_file_upload(path, mime_type = "") ⇒ Cybertrain::Test::Upload
A file to send in params: of post, patch or put, as Rails’
fixture_file_upload gives one: the request then goes as
multipart/form-data, the file under its bracketed key
(params: { post: { cover: fixture_file_upload("cover.png", "image/png") } }
is the part post[cover]) and the other params as text parts.
path is read from test/fixtures/files/ (Rails’ file_fixture_path),
or as given when it exists there; the filename sent is its last
component. Without mime_type the type is guessed from the
extension (png, jpg/jpeg, gif, webp, svg, txt, html, pdf, json, csv),
else application/octet-stream.
400 401 402 |
# File 'cybertrain/test/integration.rb', line 400 def fixture_file_upload(path, mime_type = "") Cybertrain::Test.fixture_upload(path, mime_type) end |
#follow_redirect! ⇒ Cybertrain::Response
GETs the last response’s Location (a URL on the application’s host
by its path and query), without its fragment (#comments), which a
browser never sends.
326 327 328 329 |
# File 'cybertrain/test/integration.rb', line 326 def follow_redirect! location = Cybertrain::Test::Client.redirect_location(@integration_client.response) integration_response(@integration_client.get(integration_target(location), {})) end |
#get(path, params: {}, headers: {}) ⇒ Cybertrain::Response
GET; params: go in the query string.
260 261 262 |
# File 'cybertrain/test/integration.rb', line 260 def get(path, params: {}, headers: {}) integration_response(@integration_client.get(with_query(integration_target(path), params), header_names(headers))) end |
#head(path, params: {}, headers: {}) ⇒ Cybertrain::Response
HEAD: the GET route answers and the body is dropped, as the server
drops it; params: go in the query string.
311 312 313 314 315 |
# File 'cybertrain/test/integration.rb', line 311 def head(path, params: {}, headers: {}) res = @integration_client.request("HEAD", with_query(integration_target(path), params), "", header_names(headers)) res.body = "" integration_response(res) end |
#patch(path, params: {}, headers: {}) ⇒ Cybertrain::Response
PATCH; params: go in the body as a form.
280 281 282 |
# File 'cybertrain/test/integration.rb', line 280 def patch(path, params: {}, headers: {}) integration_response(form_request("PATCH", integration_target(path), params, header_names(headers))) end |
#post(path, params: {}, headers: {}) ⇒ Cybertrain::Response
POST; params: go in the body as a form.
270 271 272 |
# File 'cybertrain/test/integration.rb', line 270 def post(path, params: {}, headers: {}) integration_response(form_request("POST", integration_target(path), params, header_names(headers))) end |
#put(path, params: {}, headers: {}) ⇒ Cybertrain::Response
PUT; params: go in the body as a form.
290 291 292 |
# File 'cybertrain/test/integration.rb', line 290 def put(path, params: {}, headers: {}) integration_response(form_request("PUT", integration_target(path), params, header_names(headers))) end |
#response ⇒ Cybertrain::Response
The last response (an empty 200 before the first request).
352 353 354 |
# File 'cybertrain/test/integration.rb', line 352 def response @integration_client.response end |
#with_forgery_protection { ... } ⇒ nil
Runs the block with forgery protection on, as Rails’
ActionController::Base.allow_forgery_protection = true does: its
non-GET requests must carry the token from a page the test rendered
(else 403). Everything is put back when the block ends, also when an
assertion in it fails.
344 345 346 347 |
# File 'cybertrain/test/integration.rb', line 344 def with_forgery_protection Cybertrain::Test.forgery_protection_during(@integration_client) { yield } nil end |