summaryrefslogtreecommitdiff
path: root/test/support/assert_responses.rb
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2014-06-09 11:00:28 +0200
committerAzul <azul@leap.se>2014-06-09 11:00:28 +0200
commit728d6d3985126c2890638bb2ee24020fa0e36a80 (patch)
tree1fcbb560b0103123d49fb953e86fdb960ee5dd13 /test/support/assert_responses.rb
parentb9174fdc9d9bd403d9a16650bafc4715e3dbf2d4 (diff)
parent9fa52ed80d71ec56ed5acf18dfd63bd03b201cc5 (diff)
Merge tag '0.5.2'
Diffstat (limited to 'test/support/assert_responses.rb')
-rw-r--r--test/support/assert_responses.rb30
1 files changed, 25 insertions, 5 deletions
diff --git a/test/support/assert_responses.rb b/test/support/assert_responses.rb
index b01166f..19c2768 100644
--- a/test/support/assert_responses.rb
+++ b/test/support/assert_responses.rb
@@ -8,21 +8,27 @@ module AssertResponses
@response || last_response
end
- def assert_attachement_filename(name)
- assert_equal %Q(attachment; filename="#{name}"),
- get_response.headers["Content-Disposition"]
+ def content_type
+ get_response.content_type.to_s.split(';').first
end
def json_response
+ return nil unless content_type == 'application/json'
response = JSON.parse(get_response.body)
response.respond_to?(:with_indifferent_access) ?
response.with_indifferent_access :
response
end
+ def assert_text_response(body = nil)
+ assert_equal 'text/plain', content_type
+ unless body.nil?
+ assert_equal body, get_response.body
+ end
+ end
+
def assert_json_response(object)
- assert_equal 'application/json',
- get_response.content_type.to_s.split(';').first
+ assert_equal 'application/json', content_type
if object.is_a? Hash
object.stringify_keys! if object.respond_to? :stringify_keys!
assert_equal object, json_response
@@ -35,6 +41,20 @@ module AssertResponses
object.stringify_keys! if object.respond_to? :stringify_keys!
assert_json_response :errors => object
end
+
+ # checks for the presence of a key in a json response
+ # or a string in a text response
+ def assert_response_includes(string_or_key)
+ response = json_response || get_response.body
+ assert response.include?(string_or_key),
+ "response should have included #{string_or_key}"
+ end
+
+ def assert_attachement_filename(name)
+ assert_equal %Q(attachment; filename="#{name}"),
+ get_response.headers["Content-Disposition"]
+ end
+
end
class ::ActionController::TestCase