validate json-ld with google structured data testing tool

This commit is contained in:
Ben Balter 2017-04-11 17:49:57 -05:00
parent f048342e22
commit 2a00c25c7b
No known key found for this signature in database
GPG Key ID: DBB67C246AD356C4
2 changed files with 40 additions and 0 deletions

View File

@ -149,4 +149,22 @@ RSpec.describe Jekyll::SeoTag::JSONLD do
expect(subject).to have_key("url")
expect(subject["url"]).to eql("/page.html")
end
context "validating" do
let(:html) do
"<script type=\"application/ld+json\">#{subject.to_json}</script>"
end
let(:image) { { "path" => "image", "height" => "100%", "width" => "10px" } }
let(:config) do
{
"logo" => "logo",
"timezone" => "America/New_York",
"url" => "http://example.com",
}
end
it "validates" do
expect(html).to be_valid_json_ld
end
end
end

View File

@ -2,9 +2,31 @@ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
require "jekyll"
require "jekyll-seo-tag"
require "html-proofer"
require "uri"
require "net/http"
ENV["JEKYLL_LOG_LEVEL"] = "error"
RSpec::Matchers.define :be_valid_json_ld do |_expected|
match do |actual|
validate_json_ld(actual)["errors"].empty?
end
failure_message do |actual|
validate_json_ld(actual)["errors"]
end
def validate_json_ld(html)
params = { "html" => html }
url = URI.parse("https://search.google.com/structured-data/testing-tool/validate")
response = Net::HTTP.post_form(url, params)
JSON.parse(response.body.split("\n")[1])
rescue
puts "Unable to validate JSON"
{ "errors" => [] }
end
end
def dest_dir
File.expand_path("../tmp/dest", File.dirname(__FILE__))
end