|
3 | 3 | *
|
4 | 4 | * @author goofballLogic
|
5 | 5 | */
|
6 |
| - |
7 |
| -var jsonld = require( "../js/jsonld" ); |
8 |
| -var assert = require( "assert" ); |
9 |
| - |
10 |
| -describe( "For the node.js document loader", function() { |
11 |
| - |
12 |
| - var documentLoaderType = "node"; |
13 |
| - var requestMock = function( options, callback ) { |
14 |
| - |
15 |
| - requestMock.calls.push( [].slice.call( arguments, 0 ) ); // store these for later inspection |
16 |
| - callback( null, { headers: {} }, "" ); |
17 |
| - |
18 |
| - }; |
19 |
| - |
20 |
| - describe( "When built with no options specified", function() { |
21 |
| - |
22 |
| - var options = {}; |
23 |
| - it( "loading should work", function( callback ) { |
24 |
| - |
25 |
| - jsonld.useDocumentLoader( documentLoaderType ); |
26 |
| - jsonld.expand( "http://schema.org/", callback ); |
27 |
| - |
28 |
| - } ); |
29 |
| - |
30 |
| - } ); |
31 |
| - |
32 |
| - describe( "When built with no explicit headers", function() { |
33 |
| - |
34 |
| - var options = { request: requestMock }; |
35 |
| - |
36 |
| - it( "loading should pass just the ld Accept header", function( callback ) { |
37 |
| - |
38 |
| - jsonld.useDocumentLoader( documentLoaderType, options ); |
39 |
| - requestMock.calls = []; |
40 |
| - var iri = "http://some.thing.test.com/my-thing.jsonld"; |
41 |
| - jsonld.documentLoader( iri, e => { |
42 |
| - |
43 |
| - if( e ) { callback( e ); } |
44 |
| - else { |
45 |
| - |
46 |
| - var actualOptions = ( requestMock.calls[ 0 ] || {} )[ 0 ] || {}; |
47 |
| - var actualHeaders = actualOptions.headers; |
48 |
| - var expectedHeaders = { "Accept": "application/ld+json, application/json" }; |
49 |
| - assert.deepEqual( actualHeaders, expectedHeaders ); |
50 |
| - callback(); |
51 |
| - |
52 |
| - } |
53 |
| - |
54 |
| - } ); |
55 |
| - |
56 |
| - } ); |
57 |
| - |
58 |
| - } ); |
59 |
| - |
60 |
| - describe( "When built using options containing a headers object", function() { |
61 |
| - |
62 |
| - var options = { request: requestMock }; |
63 |
| - options.headers = { |
64 |
| - |
65 |
| - "x-test-header-1": "First value", |
66 |
| - "x-test-two": 2.34, |
67 |
| - "Via": "1.0 fred, 1.1 example.com (Apache/1.1)", |
68 |
| - "Authorization": "Bearer d783jkjaods9f87o83hj" |
69 |
| - |
70 |
| - }; |
71 |
| - |
72 |
| - it( "loading should pass the headers through on the request", function( callback ) { |
73 |
| - |
74 |
| - jsonld.useDocumentLoader( documentLoaderType, options ); |
75 |
| - requestMock.calls = []; |
76 |
| - var iri = "http://some.thing.test.com/my-thing.jsonld"; |
77 |
| - jsonld.documentLoader( iri, e => { |
78 |
| - |
79 |
| - if( e ) { callback( e ); } |
80 |
| - else { |
81 |
| - |
82 |
| - var actualOptions = ( requestMock.calls[ 0 ] || {} )[ 0 ] || {}; |
83 |
| - var actualHeaders = actualOptions.headers; |
84 |
| - var expectedHeaders = Object.assign( |
85 |
| - |
86 |
| - { "Accept": "application/ld+json, application/json" }, |
87 |
| - options.headers |
88 |
| - |
89 |
| - ); |
90 |
| - assert.deepEqual( actualHeaders, expectedHeaders ); |
91 |
| - callback(); |
92 |
| - |
93 |
| - } |
94 |
| - |
95 |
| - } ); |
96 |
| - |
97 |
| - } ); |
98 |
| - |
99 |
| - } ); |
100 |
| - |
101 |
| - describe( "When built using headers that already contain an Accept header", function() { |
102 |
| - |
103 |
| - var options = { request: requestMock }; |
104 |
| - options.headers = { |
105 |
| - |
106 |
| - "x-test-header-3": "Third value", |
107 |
| - "Accept": "video/mp4" |
108 |
| - |
109 |
| - }; |
110 |
| - |
111 |
| - it( "constructing the document loader should fail", function() { |
112 |
| - |
113 |
| - var expectedMessage = "Accept header may not be specified as an option; only \"application/ld+json, application/json\" is supported."; |
114 |
| - assert.throws( |
115 |
| - |
116 |
| - jsonld.useDocumentLoader.bind( jsonld, documentLoaderType, options ), |
117 |
| - err => { |
118 |
| - |
119 |
| - assert.ok( err instanceof RangeError, "A range error should be thrown" ); |
120 |
| - assert.equal( err.message, expectedMessage ); |
121 |
| - return true; |
122 |
| - |
123 |
| - } |
124 |
| - |
125 |
| - ); |
126 |
| - |
127 |
| - } ); |
128 |
| - |
129 |
| - } ); |
130 |
| - |
131 |
| -} ); |
| 6 | +var jsonld = require('../js/jsonld'); |
| 7 | +var assert = require('assert'); |
| 8 | + |
| 9 | +describe('For the node.js document loader', function() { |
| 10 | + var documentLoaderType = 'node'; |
| 11 | + var requestMock = function(options, callback) { |
| 12 | + // store these for later inspection |
| 13 | + requestMock.calls.push([].slice.call(arguments, 0)); |
| 14 | + callback(null, { headers: {} }, ''); |
| 15 | + }; |
| 16 | + |
| 17 | + describe('When built with no options specified', function() { |
| 18 | + var options = {}; |
| 19 | + it('loading should work', function(callback) { |
| 20 | + jsonld.useDocumentLoader(documentLoaderType); |
| 21 | + jsonld.expand('http://schema.org/', callback); |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('When built with no explicit headers', function() { |
| 26 | + var options = { request: requestMock }; |
| 27 | + |
| 28 | + it('loading should pass just the ld Accept header', function(done) { |
| 29 | + jsonld.useDocumentLoader(documentLoaderType, options); |
| 30 | + requestMock.calls = []; |
| 31 | + var iri = 'http://some.thing.test.com/my-thing.jsonld'; |
| 32 | + jsonld.documentLoader(iri, function(err) { |
| 33 | + if(err) { |
| 34 | + return done(err); |
| 35 | + } |
| 36 | + var actualOptions = (requestMock.calls[0] || {})[0] || {}; |
| 37 | + var actualHeaders = actualOptions.headers; |
| 38 | + var expectedHeaders = { |
| 39 | + 'Accept': 'application/ld+json, application/json' |
| 40 | + }; |
| 41 | + assert.deepEqual(actualHeaders, expectedHeaders); |
| 42 | + done(); |
| 43 | + }); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('When built using options containing a headers object', function() { |
| 48 | + var options = { request: requestMock }; |
| 49 | + options.headers = { |
| 50 | + 'x-test-header-1': 'First value', |
| 51 | + 'x-test-two': 2.34, |
| 52 | + 'Via': '1.0 fred, 1.1 example.com (Apache/1.1)', |
| 53 | + 'Authorization': 'Bearer d783jkjaods9f87o83hj' |
| 54 | + }; |
| 55 | + |
| 56 | + it('loading should pass the headers through on the request', function(done) { |
| 57 | + jsonld.useDocumentLoader(documentLoaderType, options); |
| 58 | + requestMock.calls = []; |
| 59 | + var iri = 'http://some.thing.test.com/my-thing.jsonld'; |
| 60 | + jsonld.documentLoader(iri, function(err) { |
| 61 | + if(err) { |
| 62 | + return done(err); |
| 63 | + } |
| 64 | + var actualOptions = (requestMock.calls[0] || {})[0] || {}; |
| 65 | + var actualHeaders = actualOptions.headers; |
| 66 | + var expectedHeaders = { |
| 67 | + 'Accept': 'application/ld+json, application/json' |
| 68 | + }; |
| 69 | + for(var k in options.headers) { |
| 70 | + expectedHeaders[k] = options.headers[k]; |
| 71 | + } |
| 72 | + assert.deepEqual(actualHeaders, expectedHeaders); |
| 73 | + done(); |
| 74 | + }); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('When built using headers that already contain an Accept header', function() { |
| 79 | + var options = {request: requestMock}; |
| 80 | + options.headers = { |
| 81 | + 'x-test-header-3': 'Third value', |
| 82 | + 'Accept': 'video/mp4' |
| 83 | + }; |
| 84 | + |
| 85 | + it('constructing the document loader should fail', function() { |
| 86 | + var expectedMessage = 'Accept header may not be specified as an option; only "application/ld+json, application/json" is supported.'; |
| 87 | + assert.throws( |
| 88 | + jsonld.useDocumentLoader.bind(jsonld, documentLoaderType, options), |
| 89 | + function(err) { |
| 90 | + assert.ok(err instanceof RangeError, 'A range error should be thrown'); |
| 91 | + assert.equal(err.message, expectedMessage); |
| 92 | + return true; |
| 93 | + }); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | +}); |
0 commit comments