2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "action_dispatch/testing/assertions/response"
|
2012-01-06 14:20:26 -05:00
|
|
|
|
|
|
|
module ActionDispatch
|
|
|
|
module Assertions
|
|
|
|
class ResponseAssertionsTest < ActiveSupport::TestCase
|
|
|
|
include ResponseAssertions
|
|
|
|
|
2016-09-12 17:56:38 -04:00
|
|
|
FakeResponse = Struct.new(:response_code, :location, :body) do
|
2015-12-03 22:03:21 -05:00
|
|
|
def initialize(*)
|
|
|
|
super
|
|
|
|
self.location ||= "http://test.example.com/posts"
|
2016-09-12 17:56:38 -04:00
|
|
|
self.body ||= ""
|
2015-12-03 22:03:21 -05:00
|
|
|
end
|
|
|
|
|
2015-07-13 21:10:36 -04:00
|
|
|
[:successful, :not_found, :redirection, :server_error].each do |sym|
|
2012-01-06 14:20:26 -05:00
|
|
|
define_method("#{sym}?") do
|
|
|
|
sym == response_code
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-17 21:20:25 -05:00
|
|
|
def setup
|
|
|
|
@controller = nil
|
|
|
|
@request = nil
|
|
|
|
end
|
|
|
|
|
2012-01-06 14:20:26 -05:00
|
|
|
def test_assert_response_predicate_methods
|
|
|
|
[:success, :missing, :redirect, :error].each do |sym|
|
2016-08-06 12:54:50 -04:00
|
|
|
@response = FakeResponse.new RESPONSE_PREDICATES[sym].to_s.sub(/\?/, "").to_sym
|
2012-01-06 14:20:26 -05:00
|
|
|
assert_response sym
|
|
|
|
|
2013-12-18 02:51:24 -05:00
|
|
|
assert_raises(Minitest::Assertion) {
|
2012-01-06 14:20:26 -05:00
|
|
|
assert_response :unauthorized
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-17 10:56:08 -04:00
|
|
|
def test_assert_response_integer
|
2012-01-06 14:20:26 -05:00
|
|
|
@response = FakeResponse.new 400
|
|
|
|
assert_response 400
|
|
|
|
|
2013-12-18 02:51:24 -05:00
|
|
|
assert_raises(Minitest::Assertion) {
|
2012-01-06 14:20:26 -05:00
|
|
|
assert_response :unauthorized
|
|
|
|
}
|
|
|
|
|
2013-12-18 02:51:24 -05:00
|
|
|
assert_raises(Minitest::Assertion) {
|
2012-01-06 14:20:26 -05:00
|
|
|
assert_response 500
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_response_sym_status
|
|
|
|
@response = FakeResponse.new 401
|
|
|
|
assert_response :unauthorized
|
|
|
|
|
2013-12-18 02:51:24 -05:00
|
|
|
assert_raises(Minitest::Assertion) {
|
2012-01-06 14:20:26 -05:00
|
|
|
assert_response :ok
|
|
|
|
}
|
|
|
|
|
2013-12-18 02:51:24 -05:00
|
|
|
assert_raises(Minitest::Assertion) {
|
2012-01-06 14:20:26 -05:00
|
|
|
assert_response :success
|
|
|
|
}
|
|
|
|
end
|
2013-11-24 22:46:58 -05:00
|
|
|
|
|
|
|
def test_assert_response_sym_typo
|
|
|
|
@response = FakeResponse.new 200
|
|
|
|
|
|
|
|
assert_raises(ArgumentError) {
|
|
|
|
assert_response :succezz
|
|
|
|
}
|
|
|
|
end
|
2015-12-03 22:03:21 -05:00
|
|
|
|
2015-12-06 09:14:27 -05:00
|
|
|
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 }
|
2016-01-05 16:08:17 -05:00
|
|
|
expected = "Expected response to be a <2XX: success>,"\
|
|
|
|
" but was a <404: Not Found>"
|
|
|
|
assert_match expected, error.message
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_error_message_shows_404_when_asserted_for_200
|
|
|
|
@response = ActionDispatch::Response.new
|
|
|
|
@response.status = 404
|
|
|
|
|
|
|
|
error = assert_raises(Minitest::Assertion) { assert_response 200 }
|
|
|
|
expected = "Expected response to be a <200: OK>,"\
|
|
|
|
" but was a <404: Not Found>"
|
2015-12-06 09:14:27 -05:00
|
|
|
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
|
2016-08-06 12:54:50 -04:00
|
|
|
@response.location = "http://test.host/posts/redirect/1"
|
2015-12-06 09:14:27 -05:00
|
|
|
|
|
|
|
error = assert_raises(Minitest::Assertion) { assert_response :success }
|
2016-01-05 16:08:17 -05:00
|
|
|
expected = "Expected response to be a <2XX: success>,"\
|
|
|
|
" but was a <302: Found>" \
|
2015-12-06 09:14:27 -05:00
|
|
|
" 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
|
2016-08-06 12:54:50 -04:00
|
|
|
@response.location = "http://test.host/posts/redirect/2"
|
2015-12-03 22:03:21 -05:00
|
|
|
|
2015-12-06 09:14:27 -05:00
|
|
|
error = assert_raises(Minitest::Assertion) { assert_response 301 }
|
2016-01-05 16:08:17 -05:00
|
|
|
expected = "Expected response to be a <301: Moved Permanently>,"\
|
|
|
|
" but was a <302: Found>" \
|
2015-12-06 09:14:27 -05:00
|
|
|
" redirect to <http://test.host/posts/redirect/2>"
|
|
|
|
assert_match expected, error.message
|
2015-12-03 22:03:21 -05:00
|
|
|
end
|
2016-09-12 17:56:38 -04:00
|
|
|
|
|
|
|
def test_error_message_shows_short_response_body
|
|
|
|
@response = ActionDispatch::Response.new
|
|
|
|
@response.status = 400
|
|
|
|
@response.body = "not too long"
|
|
|
|
error = assert_raises(Minitest::Assertion) { assert_response 200 }
|
|
|
|
expected = "Expected response to be a <200: OK>,"\
|
|
|
|
" but was a <400: Bad Request>" \
|
|
|
|
"\nResponse body: not too long"
|
|
|
|
assert_match expected, error.message
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_error_message_does_not_show_long_response_body
|
|
|
|
@response = ActionDispatch::Response.new
|
|
|
|
@response.status = 400
|
|
|
|
@response.body = "not too long" * 50
|
|
|
|
error = assert_raises(Minitest::Assertion) { assert_response 200 }
|
|
|
|
expected = "Expected response to be a <200: OK>,"\
|
|
|
|
" but was a <400: Bad Request>"
|
|
|
|
assert_match expected, error.message
|
|
|
|
end
|
2012-01-06 14:20:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|