From 2a00c25c7bd3d7fa58ad32efe950abfcd48790c1 Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Tue, 11 Apr 2017 17:49:57 -0500 Subject: [PATCH] validate json-ld with google structured data testing tool --- spec/jekyll_seo_tag/json_ld_spec.rb | 18 ++++++++++++++++++ spec/spec_helper.rb | 22 ++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/spec/jekyll_seo_tag/json_ld_spec.rb b/spec/jekyll_seo_tag/json_ld_spec.rb index 88b987a..e2866b0 100644 --- a/spec/jekyll_seo_tag/json_ld_spec.rb +++ b/spec/jekyll_seo_tag/json_ld_spec.rb @@ -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 + "" + 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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bbb2756..9402edd 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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