aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/assertions/response_assertions_test.rb
blob: e76c222824a007d6544b47af08c36d71d2d75627 (plain) (blame)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require 'abstract_unit'
require 'action_dispatch/testing/assertions/response'

module ActionDispatch
  module Assertions
    class ResponseAssertionsTest < ActiveSupport::TestCase
      include ResponseAssertions

      FakeResponse = Struct.new(:response_code, :location) do
        def initialize(*)
          super
          self.location ||= "http://test.example.com/posts"
        end

        [:successful, :not_found, :redirection, :server_error].each do |sym|
          define_method("#{sym}?") do
            sym == response_code
          end
        end
      end

      def test_assert_response_predicate_methods
        [:success, :missing, :redirect, :error].each do |sym|
          @response = FakeResponse.new RESPONSE_PREDICATES[sym].to_s.sub(/\?/, '').to_sym
          assert_response sym

          assert_raises(Minitest::Assertion) {
            assert_response :unauthorized
          }
        end
      end

      def test_assert_response_fixnum
        @response = FakeResponse.new 400
        assert_response 400

        assert_raises(Minitest::Assertion) {
          assert_response :unauthorized
        }

        assert_raises(Minitest::Assertion) {
          assert_response 500
        }
      end

      def test_assert_response_sym_status
        @response = FakeResponse.new 401
        assert_response :unauthorized

        assert_raises(Minitest::Assertion) {
          assert_response :ok
        }

        assert_raises(Minitest::Assertion) {
          assert_response :success
        }
      end

      def test_assert_response_sym_typo
        @response = FakeResponse.new 200

        assert_raises(ArgumentError) {
          assert_response :succezz
        }
      end

      def test_error_message_shows_404_when_404_asserted_for_success
        @response = ActionDispatch::Response.new
        @response.status = 404

        error = assert_raises(Minitest::Assertion) { assert_response :success }
        expected = "Expected response to be a <success>, but was a <404>"
        assert_match expected, error.message
      end

      def test_error_message_shows_302_redirect_when_302_asserted_for_success
        @response = ActionDispatch::Response.new
        @response.status = 302
        @response.location = 'http://test.host/posts/redirect/1'

        error = assert_raises(Minitest::Assertion) { assert_response :success }
        expected = "Expected response to be a <success>, but was a <302>" \
                   " redirect to <http://test.host/posts/redirect/1>"
        assert_match expected, error.message
      end

      def test_error_message_shows_302_redirect_when_302_asserted_for_301
        @response = ActionDispatch::Response.new
        @response.status = 302
        @response.location = 'http://test.host/posts/redirect/2'

        error = assert_raises(Minitest::Assertion) { assert_response 301 }
        expected = "Expected response to be a <301>, but was a <302>" \
                   " redirect to <http://test.host/posts/redirect/2>"
        assert_match expected, error.message
      end
    end
  end
end