Class: ActionDispatch::IntegrationTest

Inherits:
ActiveSupport::TestCase show all
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.

Examples:

test/controllers/articles_controller_test.rb

require "test_helper"

class ArticlesControllerTest < ActionDispatch::IntegrationTest
  test "creates an article" do
    assert_difference -> { Article.count }, 1 do
      post articles_path, params: { article: { title: "New", body: "A body long enough" } }
    end
    assert_redirected_to article_path(Article.last)
    follow_redirect!
    assert_response :success
  end
end

Instance Method Summary collapse

Methods inherited from ActiveSupport::TestCase

fixtures

Methods inherited from Cybertrain::Test::Base

#setup, #teardown

Constructor Details

#initialize ⇒ IntegrationTest

A new client on Cybertrain::Test.app: an empty cookie jar.

Raises:

  • (RuntimeError) —

    when no application was booted



245
246
247
248
249
250
251
252
# File 'cybertrain/test/integration.rb', line 245

def initialize
  app = Cybertrain::Test.app
  if app.nil?
    message = Cybertrain::Test::NO_APP
    raise message
  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.

Parameters:

  • path (String)

Returns:

  • (nil)


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, …).

Parameters:

Returns:

  • (nil)


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 cookies
  @integration_client.cookies
end

#delete(path, params: {}, headers: {}) ⇒ Cybertrain::Response

DELETE; params: go in the body as a form.

Parameters:

  • path (String)
  • params (Hash) (defaults to: {})
  • headers (Hash{String => String}) (defaults to: {})

Returns:



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.

Examples:

post posts_url, params: { post: { title: "Hi", cover: fixture_file_upload("cover.png", "image/png") } }

Parameters:

  • path (String)
  • mime_type (String) (defaults to: "")

Returns:

Raises:

  • (ArgumentError) —

    when neither test/fixtures/files/ nor path is a file



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.

Returns:

Raises:

  • (Cybertrain::Test::AssertionFailed) —

    when the last response was not a redirect (follow_redirect!: the last response was 200, not a redirect (3xx with a Location))

  • (ArgumentError) —

    when the Location is on another host



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.

Parameters:

  • path (String)
  • params (Hash) (defaults to: {}) —

    Strings or nested Hashes (Cybertrain::Test.flatten_params)

  • headers (Hash{String => String}) (defaults to: {})

Returns:



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.

Parameters:

  • path (String)
  • params (Hash) (defaults to: {})
  • headers (Hash{String => String}) (defaults to: {})

Returns:



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.

Parameters:

  • path (String)
  • params (Hash) (defaults to: {})
  • headers (Hash{String => String}) (defaults to: {})

Returns:



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.

Parameters:

  • path (String)
  • params (Hash) (defaults to: {})
  • headers (Hash{String => String}) (defaults to: {})

Returns:



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.

Parameters:

  • path (String)
  • params (Hash) (defaults to: {})
  • headers (Hash{String => String}) (defaults to: {})

Returns:



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.

Examples:

with_forgery_protection do
  post articles_path, params: { article: { title: "x" } }
  assert_response :forbidden
end

Yields:

  • the requests to check

Returns:

  • (nil)


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