-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathnode-document-loader-tests.js
100 lines (91 loc) · 3.25 KB
/
node-document-loader-tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Local tests for the node.js document loader
*
* @author goofballLogic
*/
var jsonld = require('../js/jsonld');
var assert = require('assert');
describe('For the node.js document loader', function() {
var documentLoaderType = 'node';
var requestMock = function(options, callback) {
// store these for later inspection
requestMock.calls.push([].slice.call(arguments, 0));
callback(null, { headers: {} }, '');
};
describe('When built with no options specified', function() {
var options = {};
it('loading should work', function(done) {
jsonld.useDocumentLoader(documentLoaderType);
jsonld.expand('http://schema.org/', function(err, expanded) {
assert.ifError(err);
done();
});
});
});
describe('When built with no explicit headers', function() {
var options = { request: requestMock };
it('loading should pass just the ld Accept header', function(done) {
jsonld.useDocumentLoader(documentLoaderType, options);
requestMock.calls = [];
var iri = 'http://some.thing.test.com/my-thing.jsonld';
jsonld.documentLoader(iri, function(err) {
if(err) {
return done(err);
}
var actualOptions = (requestMock.calls[0] || {})[0] || {};
var actualHeaders = actualOptions.headers;
var expectedHeaders = {
'Accept': 'application/ld+json, application/json'
};
assert.deepEqual(actualHeaders, expectedHeaders);
done();
});
});
});
describe('When built using options containing a headers object', function() {
var options = { request: requestMock };
options.headers = {
'x-test-header-1': 'First value',
'x-test-two': 2.34,
'Via': '1.0 fred, 1.1 example.com (Apache/1.1)',
'Authorization': 'Bearer d783jkjaods9f87o83hj'
};
it('loading should pass the headers through on the request', function(done) {
jsonld.useDocumentLoader(documentLoaderType, options);
requestMock.calls = [];
var iri = 'http://some.thing.test.com/my-thing.jsonld';
jsonld.documentLoader(iri, function(err) {
if(err) {
return done(err);
}
var actualOptions = (requestMock.calls[0] || {})[0] || {};
var actualHeaders = actualOptions.headers;
var expectedHeaders = {
'Accept': 'application/ld+json, application/json'
};
for(var k in options.headers) {
expectedHeaders[k] = options.headers[k];
}
assert.deepEqual(actualHeaders, expectedHeaders);
done();
});
});
});
describe('When built using headers that already contain an invalid Accept header', function() {
var options = {request: requestMock};
options.headers = {
'x-test-header-3': 'Third value',
'Accept': 'video/mp4'
};
it('constructing the document loader should fail', function() {
var expectedMessage = 'Accept header must contains "application/ld+json, application/json".';
assert.throws(
jsonld.useDocumentLoader.bind(jsonld, documentLoaderType, options),
function(err) {
assert.ok(err instanceof RangeError, 'A range error should be thrown');
assert.equal(err.message, expectedMessage);
return true;
});
});
});
});