Class: Test::Unit::TestCase
- Inherits:
-
Object
- Object
- Test::Unit::TestCase
- Defined in:
- lib/assert_valid_markup.rb
Constant Summary collapse
- @@default_avm_options =
{ :catalog_path => File.("~/.xml-catalogs"), :validation_service => system("xmllint --version > /dev/null 2>&1") ? :local : :w3c, :dtd_validate => true }
- @@skip_validation =
false
Class Method Summary collapse
-
.assert_all_valid_markup ⇒ Object
Class-level method to to turn on validation for the response from any successful html request via “get”.
-
.assert_valid_markup(*actions) ⇒ Object
Class-level method to quickly create validation tests for a bunch of actions at once.
Instance Method Summary collapse
-
#assert_valid_markup(fragment = @response.body, options = {}) ⇒ Object
Assert that markup (html/xhtml) is valid according the W3C validator web service.
- #local_validate(xmldata, dtd_validate, catalog_path) ⇒ Object
-
#skip_markup_validation ⇒ Object
Allows one to skip validation for the given block - useful when you use assert_all_valid_markup and need to only skip validation for a handful of tests.
- #w3c_validate(fragment, dtd_validate) ⇒ Object
Class Method Details
.assert_all_valid_markup ⇒ Object
Class-level method to to turn on validation for the response from any successful html request via “get”
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/assert_valid_markup.rb', line 62 def self.assert_all_valid_markup self.class_eval do # automatically check markup for all successfull GETs def get_with_assert_valid_markup(*args) get_without_assert_valid_markup(*args) assert_valid_markup if ! @@skip_validation && @request.format.html? && @response.success? end alias_method_chain :get, :assert_valid_markup end end |
.assert_valid_markup(*actions) ⇒ Object
Class-level method to quickly create validation tests for a bunch of actions at once. For example, if you have a FooController with three actions, just add one line to foo_controller_test.rb:
assert_valid_markup :bar, :baz, :qux
If you pass :but_first => :something, #something will be called at the beginning of each test case
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/assert_valid_markup.rb', line 48 def self.assert_valid_markup(*actions) = actions.find { |i| i.kind_of? Hash } actions.delete_if { |i| i.kind_of? Hash } actions.each do |action| toeval = "def test_#{action}_valid_markup\n" toeval << "#{[:but_first].id2name}\n" if and [:but_first] toeval << "get :#{action}\n" toeval << "assert_valid_markup\n" toeval << "end\n" class_eval toeval end end |
Instance Method Details
#assert_valid_markup(fragment = @response.body, options = {}) ⇒ Object
Assert that markup (html/xhtml) is valid according the W3C validator web service. By default, it validates the contents of @response.body, which is set after calling one of the get/post/etc helper methods. You can also pass it a string to be validated. Validation errors, if any, will be included in the output. The response from the validator service will be cached in the system temp directory to minimize duplicate calls.
For example, if you have a FooController with an action Bar, put this in foo_controller_test.rb:
def
get :bar
assert_valid_markup
end
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/assert_valid_markup.rb', line 31 def assert_valid_markup(fragment=@response.body, ={}) opts = @@default_avm_options.merge() result = '' if opts[:validation_service] == :local result = local_validate(fragment, opts[:dtd_validate], opts[:catalog_path]) else result = w3c_validate(fragment, opts[:dtd_validate]) end assert result.empty?, result end |
#local_validate(xmldata, dtd_validate, catalog_path) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/assert_valid_markup.rb', line 86 def local_validate(xmldata, dtd_validate, catalog_path) catalog_file = "#{catalog_path}/catalog" if ! File.exists? catalog_path puts "Creating xml catalog at: #{catalog_path}" FileUtils.mkdir_p(catalog_path) out = `xmlcatalog --noout --create '#{catalog_file}' 2>&1` if $? != 0 puts out exit 1 end end ENV["XML_DEBUG_CATALOG"] = "" ENV["SGML_CATALOG_FILES"] = catalog_file tmpfile = Tempfile.new('xmllint') tmpfile.write(xmldata) tmpfile.close validation_output = `xmllint --catalogs --memory --noout #{dtd_validate ? '--valid' : ''} #{tmpfile.path} 2>&1`.lines.to_a ENV.delete("XML_DEBUG_CATALOG") added_to_catalog = false last_sysid = "" validation_output.each do |line| line.chomp! if match = line.match(/Resolve: pubID (.*) sysID (.*)/) pubid = match[1] sysid = match[2] localdtd = "#{catalog_path}/#{sysid.split('/').last}" if ! File.exists? localdtd puts "Adding xml catalog resource\n\tpublic id: '#{pubid}'\n\turi: '#{sysid}'\n\tfile: '#{localdtd}'" if sysid =~ /^file:/ basename = sysid.split('/').last dirname = last_sysid.gsub(/\/[^\/]*$/, '') sysid = "#{dirname}/#{basename}" puts "Using sysid relative to parent: #{sysid}" end sysid_contents = open(sysid, 'r', 0, 'User-Agent' => 'assert_valid_markup').read() open(localdtd, "w") {|f| f.write(sysid_contents)} added_to_catalog = true out = `xmlcatalog --noout --add 'public' '#{pubid}' 'file://#{localdtd}' '#{catalog_file}' 2>&1` if $? != 0 puts out exit 1 end end last_sysid = sysid end end if added_to_catalog return local_validate(xmldata, dtd_validate, catalog_path) else validation_failed = validation_output.grep(/^#{Regexp.escape(tmpfile.path)}:/) msg = [] validation_failed.each do |l| msg << l.gsub(/^[^:]*:/, "Invalid markup: line ") if l =~ /^[^:]*:(\d+)/ line = $1.to_i ((line - 1)..(line + 1)).each do |ln| msg << "\t#{ln}: #{xmldata.lines.to_a[ln-1]}" end end end return msg.join("\n") end end |
#skip_markup_validation ⇒ Object
Allows one to skip validation for the given block - useful when you use assert_all_valid_markup and need to only skip validation for a handful of tests
77 78 79 80 81 82 83 84 |
# File 'lib/assert_valid_markup.rb', line 77 def skip_markup_validation begin @@skip_validation = true yield ensure @@skip_validation = false end end |
#w3c_validate(fragment, dtd_validate) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/assert_valid_markup.rb', line 154 def w3c_validate(fragment, dtd_validate) validation_result = '' begin filename = File.join Dir::tmpdir, 'markup.' + Digest::MD5.hexdigest(fragment).to_s if ! ENV['NO_CACHE_VALIDATION'] response = File.open filename {|f| Marshal.load(f) } unless ENV['NO_CACHE_VALIDATION'] rescue nil end if ! response response = Net::HTTP.start('validator.w3.org').post2('/check', "fragment=#{CGI.escape(fragment)}&output=xml") File.open filename, 'w+' do |f| Marshal.dump response, f end end markup_is_valid = response['x-w3c-validator-status']=='Valid' if ! markup_is_valid doc = XmlSimple.xml_in(response.body) validation_result = doc['messages'][0]['msg'].collect{ |m| "Invalid markup: line #{m['line']}: #{CGI.unescapeHTML(m['content'])}" }.join("\n") end rescue SocketError # if we can't reach the validator service, just let the test pass puts "WARNING: Could not reach w3c validator service" end return validation_result end |