request_mime(:fbml) do
get :list
assert_response_mime(:fbml)
end
request_mime("text/xml") do
get :list
assert_response_mime("text/xml")
end
Just include it in your test_helper.rb file in test/
class Test::Unit::TestCase
include Threecglabs::MimeTestHelpers
end
Here's "mime_test_helpers.rb". Just throw it in lib/
module Threecglabs
module MimeTestHelpers
def self.included(mod)
mod.class_eval do
include MimeRequest
include MimeAssertions
end
end
module MimeRequest
# changes the mime type of the request within the block
#
# request_mime(:fbml) do
# get :list
# assert_response_mime(:fbml)
# end
def request_mime(mime_type_name_or_extension)
if mime_type_name_or_extension.kind_of?(String)
mime_type = Mime::Type.lookup(mime_type_name_or_extension)
elsif mime_type_name_or_extension.kind_of?(Symbol)
mime_type = Mime::Type.lookup_by_extension(mime_type_name_or_extension.to_s)
else
raise ArgumentError.new("mime type must be string or symbol")
end
old_mime_type = @request.accepts
@request.accept = mime_type.to_s
yield
@request.accept = old_mime_type
end
end
# These are assertions to test respond_to, whether they return a specific mime type
# as a response to a request
module MimeAssertions
# Helps out with response testing, by letting to assert that the most recently-made
# request responded with a particular MIME extension, like :html, :fbml, :xml, etc.
def assert_response_mime(expected_mime_type_ext)
expected_mime_type = Mime::Type.lookup_by_extension(expected_mime_type_ext.to_s)
# Mime::Type.parse doesn't parse accept parameters correctly, therefore
# we account for having multiple types in the accept
response_mime_types = @response.headers['type'].split(/,\s*/).map do |accept_type|
mime_type_name = accept_type.split(/;\s*/).first
Mime::Type.parse(mime_type_name)
end
assert_block("Responded with #{response_mime_types.map(&:to_s).inspect} when expecting #{expected_mime_type}") {
response_mime_types.any? { |response_mime_type| expected_mime_type == response_mime_type }
}
end
end
end
end
Snippet!
No comments:
Post a Comment