Skip to content

Commit

Permalink
Remove dependency on schema.org for conneg tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Aug 28, 2023
1 parent cb5ef84 commit e119ff0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 178 deletions.
191 changes: 20 additions & 171 deletions spec/conneg_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require_relative 'spec_helper'
require 'rack/linkeddata'
require 'rack/rdf'
require 'rack/test'

describe JSON::LD::ContentNegotiation do
Expand All @@ -12,6 +12,25 @@
described_class.new(double("Target Rack Application", :call => [200, {}, @results || "A String"]))
end

let(:example_context) do
JSON::LD::API::RemoteDocument.new('{
"@context": {
"@vocab": "http://example.com/",
"id": "@id",
"type": "@type"
}
}', documentUrl: "http://example.com")
end

before do
JSON::LD::Writer.default_context = 'http://example.com/context'
allow(JSON::LD::API).to receive(:documentLoader).with('http://example.com/context', any_args).and_yield(example_context)
end

after do
JSON::LD::Writer.default_context = nil
end

describe "#parse_accept_header" do
{
"application/n-triples, application/ld+json;q=0.5" => %w[application/ld+json],
Expand Down Expand Up @@ -40,15 +59,6 @@
end

describe "#call" do
let(:schema_context) do
JSON::LD::API::RemoteDocument.new('{
"@context": {
"@vocab": "http://schema.org/",
"id": "@id",
"type": "@type"
}
}', documentUrl: "http://schema.org")
end
let(:frame) do
JSON::LD::API::RemoteDocument.new('{
"@context": {
Expand All @@ -74,7 +84,6 @@
end

before do
allow(JSON::LD::API).to receive(:documentLoader).with("http://schema.org", any_args).and_yield(schema_context)
allow(JSON::LD::API).to receive(:documentLoader).with("http://conneg.example.com/context",
any_args).and_yield(context)
allow(JSON::LD::API).to receive(:documentLoader).with("http://conneg.example.com/frame",
Expand Down Expand Up @@ -216,163 +225,3 @@
end
end
end

describe Rack::LinkedData::ContentNegotiation do
include Rack::Test::Methods
let(:logger) { RDF::Spec.logger }

let(:app) do
graph = RDF::NTriples::Reader.new(%(
<http://example.org/library> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/vocab#Library> .
<http://example.org/library> <http://example.org/vocab#contains> <http://example.org/library/the-republic> .
<http://example.org/library/the-republic> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/vocab#Book> .
<http://example.org/library/the-republic> <http://purl.org/dc/elements/1.1/title> "The Republic" .
<http://example.org/library/the-republic> <http://purl.org/dc/elements/1.1/creator> "Plato" .
<http://example.org/library/the-republic> <http://example.org/vocab#contains> <http://example.org/library/the-republic#introduction> .
<http://example.org/library/the-republic#introduction> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/vocab#Chapter> .
<http://example.org/library/the-republic#introduction> <http://purl.org/dc/elements/1.1/title> "The Introduction" .
<http://example.org/library/the-republic#introduction> <http://purl.org/dc/elements/1.1/description> "An introductory chapter on The Republic." .
))
described_class.new(double("Target Rack Application", :call => [200, {}, graph]), {})
end

describe "#call" do
let(:schema_context) do
JSON::LD::API::RemoteDocument.new('{
"@context": {
"@vocab": "http://schema.org/",
"id": "@id",
"type": "@type"
}
}', documentUrl: "http://schema.org")
end
let(:frame) do
JSON::LD::API::RemoteDocument.new('{
"@context": {
"dc": "http://purl.org/dc/elements/1.1/",
"ex": "http://example.org/vocab#"
},
"@type": "ex:Library",
"ex:contains": {
"@type": "ex:Book",
"ex:contains": {
"@type": "ex:Chapter"
}
}
}', documentUrl: "http://conneg.example.com/frame")
end
let(:context) do
JSON::LD::API::RemoteDocument.new('{
"@context": {
"dc": "http://purl.org/dc/elements/1.1/",
"ex": "http://example.org/vocab#"
}
}', documentUrl: "http://conneg.example.com/context")
end

before do
allow(JSON::LD::API).to receive(:documentLoader).with("http://schema.org", any_args).and_yield(schema_context)
allow(JSON::LD::API).to receive(:documentLoader).with("http://conneg.example.com/context",
any_args).and_yield(context)
allow(JSON::LD::API).to receive(:documentLoader).with("http://conneg.example.com/frame",
any_args).and_yield(frame)
end

{
"application/json" => LIBRARY_FLATTENED_EXPANDED,
"application/ld+json" => LIBRARY_FLATTENED_EXPANDED,
%(application/ld+json;profile=http://www.w3.org/ns/json-ld#expanded) =>
LIBRARY_FLATTENED_EXPANDED,

%(application/ld+json;profile=http://www.w3.org/ns/json-ld#compacted) =>
LIBRARY_FLATTENED_COMPACTED_DEFAULT,

%(application/ld+json;profile=http://www.w3.org/ns/json-ld#flattened) =>
LIBRARY_FLATTENED_EXPANDED,
%(application/ld+json;profile="http://www.w3.org/ns/json-ld#flattened http://www.w3.org/ns/json-ld#expanded") =>
LIBRARY_FLATTENED_EXPANDED,
%(application/ld+json;profile="http://www.w3.org/ns/json-ld#expanded http://www.w3.org/ns/json-ld#flattened") =>
LIBRARY_FLATTENED_EXPANDED,

%(application/ld+json;profile="http://www.w3.org/ns/json-ld#flattened http://www.w3.org/ns/json-ld#compacted") =>
LIBRARY_FLATTENED_COMPACTED_DEFAULT,
%(application/ld+json;profile="http://www.w3.org/ns/json-ld#compacted http://www.w3.org/ns/json-ld#flattened") =>
LIBRARY_FLATTENED_COMPACTED_DEFAULT

}.each do |accepts, result|
context accepts do
before do
get '/', {}, { "HTTP_ACCEPT" => accepts }
end

it "status" do
expect(last_response.status).to satisfy("200 or 406") { |x| [200, 406].include?(x) }
end

it "sets content type" do
expect(last_response.content_type).to eq(last_response.status == 406 ? 'text/plain' : 'application/ld+json')
end

it "returns serialization" do
if last_response.status == 406
expect(last_response.body).to eq result
else
expect(JSON.parse(last_response.body)).to produce_jsonld(result, logger)
end
end
end
end

context "with Link" do
{
"with context" => {
accept: %(application/ld+json),
link: %(<http://conneg.example.com/context> rel="http://www.w3.org/ns/json-ld#context"),
result: LIBRARY_FLATTENED_COMPACTED
},
"compacted with context" => {
accept: %(application/ld+json;profile=http://www.w3.org/ns/json-ld#compacted),
link: %(<http://conneg.example.com/context> rel="http://www.w3.org/ns/json-ld#context"),
result: LIBRARY_FLATTENED_COMPACTED
},
"flattened and compacted with context" => {
accept: %(application/ld+json;profile="http://www.w3.org/ns/json-ld#flattened http://www.w3.org/ns/json-ld#compacted"),
link: %(<http://conneg.example.com/context> rel="http://www.w3.org/ns/json-ld#context"),
result: LIBRARY_FLATTENED_COMPACTED
},
"compacted and flattened with context" => {
accept: %(application/ld+json;profile="http://www.w3.org/ns/json-ld#compacted http://www.w3.org/ns/json-ld#flattened"),
link: %(<http://conneg.example.com/context> rel="http://www.w3.org/ns/json-ld#context"),
result: LIBRARY_FLATTENED_COMPACTED
},
"framed with frame" => {
accept: %(application/ld+json;profile=http://www.w3.org/ns/json-ld#framed),
link: %(<http://conneg.example.com/frame> rel="http://www.w3.org/ns/json-ld#frame"),
result: LIBRARY_FRAMED
}
}.each do |name, params|
context name do
before do
get '/', {}, { "HTTP_ACCEPT" => params[:accept], "HTTP_LINK" => params[:link] }
end

it "status" do
expect(last_response.status).to satisfy("be 200 or 406") { |x| [200, 406].include?(x) }
end

it "sets content type" do
expect(last_response.content_type).to eq(last_response.status == 406 ? 'text/plain' : 'application/ld+json')
end

it "returns serialization" do
if last_response.status == 406
expect(last_response.body).to eq params[:result]
else
expect(JSON.parse(last_response.body)).to produce_jsonld(params[:result], logger)
end
end
end
end
end
end
end
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def replace_nodes(object, bijection)
]))

LIBRARY_COMPACTED_DEFAULT = JSON.parse(%({
"@context": "http://schema.org",
"@context": "http://example.com/context",
"@graph": [
{
"id": "http://example.org/library",
Expand Down Expand Up @@ -213,7 +213,7 @@ def replace_nodes(object, bijection)
]))

LIBRARY_FLATTENED_COMPACTED_DEFAULT = JSON.parse(%({
"@context": "http://schema.org",
"@context": "http://example.com/context",
"@graph": [
{
"id": "http://example.org/library",
Expand Down
8 changes: 3 additions & 5 deletions spec/suite_http_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require_relative 'spec_helper'
require 'rack/linkeddata'
require 'rack/rdf'
require 'rack/test'

begin
Expand All @@ -15,10 +15,8 @@

let(:app) do
JSON::LD::ContentNegotiation.new(
Rack::LinkedData::ContentNegotiation.new(
double("Target Rack Application", :call => [200, {}, @results]),
{}
)
double("Target Rack Application", :call => [200, {}, @results]),
{}
)
end

Expand Down

0 comments on commit e119ff0

Please sign in to comment.