summaryrefslogtreecommitdiff
path: root/test/support/rack_test.rb
blob: 83adf6cf9ff0b47585ad195e1653d5d6f4e09b2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require_relative 'assert_responses'

class RackTest < ActiveSupport::TestCase
  include Rack::Test::Methods
  include Warden::Test::Helpers

  CONFIG_RU = (Rails.root + 'config.ru').to_s
  OUTER_APP = Rack::Builder.parse_file(CONFIG_RU).first

  def app
    OUTER_APP
  end

  def assert_access_denied
    assert_json_response('error' => I18n.t(:not_authorized))
    assert_response :forbidden
  end

  def assert_login_required
    assert_json_response('error' => I18n.t(:not_authorized_login))
    assert_response :unauthorized
  end

  # inspired by rails 4
  # -> actionpack/lib/action_dispatch/testing/assertions/response.rb
  def assert_response(type, message = nil)
    # RackTest does not know @response
    response_code = last_response.status
    message ||= "Expected response to be a <#{type}>, but was <#{response_code}>"

    if Symbol === type
      if [:success, :missing, :redirect, :error].include?(type)
        assert last_response.send("#{type}?"), message
      else
        code = Rack::Utils::SYMBOL_TO_STATUS_CODE[type]
        assert_equal code, response_code, message
      end
    else
      assert_equal type, response_code, message
    end
  end

end