summaryrefslogtreecommitdiff
path: root/core/lib/extensions/testing.rb
blob: aad7fc1dbfa5293c7298e7a719221612eb7ff218 (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
44
45
46
module LeapWebCore
  module AssertResponses

    # response that works with different TestCases:
    # ActionController::TestCase has @response
    # ActionDispatch::IntegrationTest has @response
    # Rack::Test::Methods defines last_response
    def get_response
      @response || last_response
    end

    def assert_attachement_filename(name)
      assert_equal %Q(attachment; filename="#{name}"),
        get_response.headers["Content-Disposition"]
    end

    def json_response
      response = JSON.parse(get_response.body)
      response.respond_to?(:with_indifferent_access) ?
        response.with_indifferent_access :
        response
    end

    def assert_json_response(object)
      if object.is_a? Hash
        object.stringify_keys! if object.respond_to? :stringify_keys!
        assert_equal object, json_response
      else
        assert_equal object.to_json, get_response.body
      end
    end

    def assert_json_error(object)
      object.stringify_keys! if object.respond_to? :stringify_keys!
      assert_json_response :errors => object
    end
  end
end

class ::ActionController::TestCase
  include LeapWebCore::AssertResponses
end

class ::ActionDispatch::IntegrationTest
  include LeapWebCore::AssertResponses
end