Skip to content

Instantly share code, notes, and snippets.

@gamov
Last active August 29, 2015 14:16
Show Gist options
  • Save gamov/46054d7b4c28730f04c1 to your computer and use it in GitHub Desktop.
Save gamov/46054d7b4c28730f04c1 to your computer and use it in GitHub Desktop.
# inspired by http://www.scottraymond.net/2005/9/20/validating-x-html-in-rails/
def assert_valid_markup(markup = @response.body, fragment = false)
str = if fragment
"<!doctype html><head><title>T</title></head><body>#{markup}</body></html>"
else
markup.to_str # .to_str to remove SafeBuffer stuff
end
require 'net/http'
# http://validator.w3.org/docs/api.html
response = Net::HTTP.start('validator.w3.org') do |w3c|
query = 'fragment=' + CGI.escape(str) + '&output=xml'
w3c.post2('/check', query)
end
unless response['x-w3c-validator-status'] == 'Valid'
val_folder = Rails.root.join('tmp').join('validations')
val_folder.mkdir
file = val_folder.join("#{Time.now.to_i}.html")
File.open(file, 'wb+') {|f| f.write(response.body) }
end
assert_equal 'Valid', response['x-w3c-validator-status'], "HTML Validation failed, see details here: #{file}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment