Andy Callaghan

RSpec matcher to validate sitemaps

We use the gem rspec-sitemap-matchers to validate and parse some sitemap files, but it can't validate against the sitemap standard XSD, so here's the code snippet.

./spec/support/matchers/valid_sitemap_matcher.rb

require 'rspec/expectations'

module ValidSitemapExpectations
  extend RSpec::Matchers::DSL

  sitemap_xsd       = Nokogiri::XML::Schema(File.open('sitemap.xsd'))
  sitemap_index_xsd = Nokogiri::XML::Schema(File.open('sitemapindex.xsd'))

  matcher :have_valid_sitemap_xml do
    match do |thing|
      doc = Nokogiri::XML(thing)
      sitemap_xsd.valid?(doc)
    end

    failure_message do |thing|
      doc = Nokogiri::XML(thing)
      errors = sitemap_xsd.validate(doc).map(&:message).join("\n")
      "Was not valid sitemap XML.\n\n#{errors}\n"
    end
  end

  matcher :have_valid_sitemap_index_xml do

    match do |thing|
      doc = Nokogiri::XML(thing)
      sitemap_index_xsd.valid?(doc)
    end
    failure_message do |thing|
      doc = Nokogiri::XML(thing)
      errors = sitemap_index_xsd.validate(doc).map(&:message).join("\n")
      "Was not valid sitemap index XML.\n\n#{errors}\n"
    end
  end
end

This is used in RSpec expectations to ensure that the sitemap generated is valid against the sitemap standard.