|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | require_relative 'spec_helper' |
4 | | -require 'rack/linkeddata' |
| 4 | +require 'rack/rdf' |
5 | 5 | require 'rack/test' |
6 | 6 |
|
7 | 7 | describe JSON::LD::ContentNegotiation do |
|
12 | 12 | described_class.new(double("Target Rack Application", :call => [200, {}, @results || "A String"])) |
13 | 13 | end |
14 | 14 |
|
| 15 | + let(:example_context) do |
| 16 | + JSON::LD::API::RemoteDocument.new('{ |
| 17 | + "@context": { |
| 18 | + "@vocab": "http://example.com/", |
| 19 | + "id": "@id", |
| 20 | + "type": "@type" |
| 21 | + } |
| 22 | + }', documentUrl: "http://example.com") |
| 23 | + end |
| 24 | + |
| 25 | + before do |
| 26 | + JSON::LD::Writer.default_context = 'http://example.com/context' |
| 27 | + allow(JSON::LD::API).to receive(:documentLoader).with('http://example.com/context', any_args).and_yield(example_context) |
| 28 | + end |
| 29 | + |
| 30 | + after do |
| 31 | + JSON::LD::Writer.default_context = nil |
| 32 | + end |
| 33 | + |
15 | 34 | describe "#parse_accept_header" do |
16 | 35 | { |
17 | 36 | "application/n-triples, application/ld+json;q=0.5" => %w[application/ld+json], |
|
40 | 59 | end |
41 | 60 |
|
42 | 61 | describe "#call" do |
43 | | - let(:schema_context) do |
44 | | - JSON::LD::API::RemoteDocument.new('{ |
45 | | - "@context": { |
46 | | - "@vocab": "http://schema.org/", |
47 | | - "id": "@id", |
48 | | - "type": "@type" |
49 | | - } |
50 | | - }', documentUrl: "http://schema.org") |
51 | | - end |
52 | 62 | let(:frame) do |
53 | 63 | JSON::LD::API::RemoteDocument.new('{ |
54 | 64 | "@context": { |
|
74 | 84 | end |
75 | 85 |
|
76 | 86 | before do |
77 | | - allow(JSON::LD::API).to receive(:documentLoader).with("http://schema.org", any_args).and_yield(schema_context) |
78 | 87 | allow(JSON::LD::API).to receive(:documentLoader).with("http://conneg.example.com/context", |
79 | 88 | any_args).and_yield(context) |
80 | 89 | allow(JSON::LD::API).to receive(:documentLoader).with("http://conneg.example.com/frame", |
|
216 | 225 | end |
217 | 226 | end |
218 | 227 | end |
219 | | - |
220 | | -describe Rack::LinkedData::ContentNegotiation do |
221 | | - include Rack::Test::Methods |
222 | | - let(:logger) { RDF::Spec.logger } |
223 | | - |
224 | | - let(:app) do |
225 | | - graph = RDF::NTriples::Reader.new(%( |
226 | | - <http://example.org/library> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/vocab#Library> . |
227 | | - <http://example.org/library> <http://example.org/vocab#contains> <http://example.org/library/the-republic> . |
228 | | - <http://example.org/library/the-republic> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/vocab#Book> . |
229 | | - <http://example.org/library/the-republic> <http://purl.org/dc/elements/1.1/title> "The Republic" . |
230 | | - <http://example.org/library/the-republic> <http://purl.org/dc/elements/1.1/creator> "Plato" . |
231 | | - <http://example.org/library/the-republic> <http://example.org/vocab#contains> <http://example.org/library/the-republic#introduction> . |
232 | | - <http://example.org/library/the-republic#introduction> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/vocab#Chapter> . |
233 | | - <http://example.org/library/the-republic#introduction> <http://purl.org/dc/elements/1.1/title> "The Introduction" . |
234 | | - <http://example.org/library/the-republic#introduction> <http://purl.org/dc/elements/1.1/description> "An introductory chapter on The Republic." . |
235 | | - )) |
236 | | - described_class.new(double("Target Rack Application", :call => [200, {}, graph]), {}) |
237 | | - end |
238 | | - |
239 | | - describe "#call" do |
240 | | - let(:schema_context) do |
241 | | - JSON::LD::API::RemoteDocument.new('{ |
242 | | - "@context": { |
243 | | - "@vocab": "http://schema.org/", |
244 | | - "id": "@id", |
245 | | - "type": "@type" |
246 | | - } |
247 | | - }', documentUrl: "http://schema.org") |
248 | | - end |
249 | | - let(:frame) do |
250 | | - JSON::LD::API::RemoteDocument.new('{ |
251 | | - "@context": { |
252 | | - "dc": "http://purl.org/dc/elements/1.1/", |
253 | | - "ex": "http://example.org/vocab#" |
254 | | - }, |
255 | | - "@type": "ex:Library", |
256 | | - "ex:contains": { |
257 | | - "@type": "ex:Book", |
258 | | - "ex:contains": { |
259 | | - "@type": "ex:Chapter" |
260 | | - } |
261 | | - } |
262 | | - }', documentUrl: "http://conneg.example.com/frame") |
263 | | - end |
264 | | - let(:context) do |
265 | | - JSON::LD::API::RemoteDocument.new('{ |
266 | | - "@context": { |
267 | | - "dc": "http://purl.org/dc/elements/1.1/", |
268 | | - "ex": "http://example.org/vocab#" |
269 | | - } |
270 | | - }', documentUrl: "http://conneg.example.com/context") |
271 | | - end |
272 | | - |
273 | | - before do |
274 | | - allow(JSON::LD::API).to receive(:documentLoader).with("http://schema.org", any_args).and_yield(schema_context) |
275 | | - allow(JSON::LD::API).to receive(:documentLoader).with("http://conneg.example.com/context", |
276 | | - any_args).and_yield(context) |
277 | | - allow(JSON::LD::API).to receive(:documentLoader).with("http://conneg.example.com/frame", |
278 | | - any_args).and_yield(frame) |
279 | | - end |
280 | | - |
281 | | - { |
282 | | - "application/json" => LIBRARY_FLATTENED_EXPANDED, |
283 | | - "application/ld+json" => LIBRARY_FLATTENED_EXPANDED, |
284 | | - %(application/ld+json;profile=http://www.w3.org/ns/json-ld#expanded) => |
285 | | - LIBRARY_FLATTENED_EXPANDED, |
286 | | - |
287 | | - %(application/ld+json;profile=http://www.w3.org/ns/json-ld#compacted) => |
288 | | - LIBRARY_FLATTENED_COMPACTED_DEFAULT, |
289 | | - |
290 | | - %(application/ld+json;profile=http://www.w3.org/ns/json-ld#flattened) => |
291 | | - LIBRARY_FLATTENED_EXPANDED, |
292 | | - %(application/ld+json;profile="http://www.w3.org/ns/json-ld#flattened http://www.w3.org/ns/json-ld#expanded") => |
293 | | - LIBRARY_FLATTENED_EXPANDED, |
294 | | - %(application/ld+json;profile="http://www.w3.org/ns/json-ld#expanded http://www.w3.org/ns/json-ld#flattened") => |
295 | | - LIBRARY_FLATTENED_EXPANDED, |
296 | | - |
297 | | - %(application/ld+json;profile="http://www.w3.org/ns/json-ld#flattened http://www.w3.org/ns/json-ld#compacted") => |
298 | | - LIBRARY_FLATTENED_COMPACTED_DEFAULT, |
299 | | - %(application/ld+json;profile="http://www.w3.org/ns/json-ld#compacted http://www.w3.org/ns/json-ld#flattened") => |
300 | | - LIBRARY_FLATTENED_COMPACTED_DEFAULT |
301 | | - |
302 | | - }.each do |accepts, result| |
303 | | - context accepts do |
304 | | - before do |
305 | | - get '/', {}, { "HTTP_ACCEPT" => accepts } |
306 | | - end |
307 | | - |
308 | | - it "status" do |
309 | | - expect(last_response.status).to satisfy("200 or 406") { |x| [200, 406].include?(x) } |
310 | | - end |
311 | | - |
312 | | - it "sets content type" do |
313 | | - expect(last_response.content_type).to eq(last_response.status == 406 ? 'text/plain' : 'application/ld+json') |
314 | | - end |
315 | | - |
316 | | - it "returns serialization" do |
317 | | - if last_response.status == 406 |
318 | | - expect(last_response.body).to eq result |
319 | | - else |
320 | | - expect(JSON.parse(last_response.body)).to produce_jsonld(result, logger) |
321 | | - end |
322 | | - end |
323 | | - end |
324 | | - end |
325 | | - |
326 | | - context "with Link" do |
327 | | - { |
328 | | - "with context" => { |
329 | | - accept: %(application/ld+json), |
330 | | - link: %(<http://conneg.example.com/context> rel="http://www.w3.org/ns/json-ld#context"), |
331 | | - result: LIBRARY_FLATTENED_COMPACTED |
332 | | - }, |
333 | | - "compacted with context" => { |
334 | | - accept: %(application/ld+json;profile=http://www.w3.org/ns/json-ld#compacted), |
335 | | - link: %(<http://conneg.example.com/context> rel="http://www.w3.org/ns/json-ld#context"), |
336 | | - result: LIBRARY_FLATTENED_COMPACTED |
337 | | - }, |
338 | | - "flattened and compacted with context" => { |
339 | | - accept: %(application/ld+json;profile="http://www.w3.org/ns/json-ld#flattened http://www.w3.org/ns/json-ld#compacted"), |
340 | | - link: %(<http://conneg.example.com/context> rel="http://www.w3.org/ns/json-ld#context"), |
341 | | - result: LIBRARY_FLATTENED_COMPACTED |
342 | | - }, |
343 | | - "compacted and flattened with context" => { |
344 | | - accept: %(application/ld+json;profile="http://www.w3.org/ns/json-ld#compacted http://www.w3.org/ns/json-ld#flattened"), |
345 | | - link: %(<http://conneg.example.com/context> rel="http://www.w3.org/ns/json-ld#context"), |
346 | | - result: LIBRARY_FLATTENED_COMPACTED |
347 | | - }, |
348 | | - "framed with frame" => { |
349 | | - accept: %(application/ld+json;profile=http://www.w3.org/ns/json-ld#framed), |
350 | | - link: %(<http://conneg.example.com/frame> rel="http://www.w3.org/ns/json-ld#frame"), |
351 | | - result: LIBRARY_FRAMED |
352 | | - } |
353 | | - }.each do |name, params| |
354 | | - context name do |
355 | | - before do |
356 | | - get '/', {}, { "HTTP_ACCEPT" => params[:accept], "HTTP_LINK" => params[:link] } |
357 | | - end |
358 | | - |
359 | | - it "status" do |
360 | | - expect(last_response.status).to satisfy("be 200 or 406") { |x| [200, 406].include?(x) } |
361 | | - end |
362 | | - |
363 | | - it "sets content type" do |
364 | | - expect(last_response.content_type).to eq(last_response.status == 406 ? 'text/plain' : 'application/ld+json') |
365 | | - end |
366 | | - |
367 | | - it "returns serialization" do |
368 | | - if last_response.status == 406 |
369 | | - expect(last_response.body).to eq params[:result] |
370 | | - else |
371 | | - expect(JSON.parse(last_response.body)).to produce_jsonld(params[:result], logger) |
372 | | - end |
373 | | - end |
374 | | - end |
375 | | - end |
376 | | - end |
377 | | - end |
378 | | -end |
0 commit comments