Skip to content

Commit c43f112

Browse files
committed
Fix style.
1 parent bc38da0 commit c43f112

File tree

2 files changed

+99
-134
lines changed

2 files changed

+99
-134
lines changed

js/jsonld.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,7 +1735,7 @@ jsonld.documentLoaders.jquery = function($, options) {
17351735
* maxRedirects: the maximum number of redirects to permit, none by
17361736
* default.
17371737
* request: the object which will make the request, default is
1738-
* provided by request.js
1738+
* provided by `https://www.npmjs.com/package/request`.
17391739
* headers: an array of headers which will be passed as request
17401740
* headers for the requested document. Accept is not allowed.
17411741
* usePromise: true to use a promises API, false for a
@@ -1760,11 +1760,10 @@ jsonld.documentLoaders.node = function(options) {
17601760
});
17611761
}
17621762
var headers = options.headers || {};
1763-
if( 'Accept' in headers || 'accept' in headers ) {
1764-
1765-
throw new RangeError( 'Accept header may not be specified as an option' +
1766-
'; only "' + acceptHeader + '" is supported.' );
1767-
1763+
if('Accept' in headers || 'accept' in headers) {
1764+
throw new RangeError(
1765+
'Accept header may not be specified as an option; only "' +
1766+
acceptHeader + '" is supported.');
17681767
}
17691768
return queue.wrapLoader(function(url, callback) {
17701769
loadDocument(url, [], callback);
@@ -1790,8 +1789,8 @@ jsonld.documentLoaders.node = function(options) {
17901789
if(doc !== null) {
17911790
return callback(null, doc);
17921791
}
1793-
var headers = { 'Accept': acceptHeader };
1794-
for( var k in options.headers ) { headers[ k ] = options.headers[ k ]; }
1792+
var headers = {'Accept': acceptHeader};
1793+
for(var k in options.headers) { headers[k] = options.headers[k]; }
17951794
request({
17961795
url: url,
17971796
headers: headers,

test/node-document-loader-tests.js

Lines changed: 92 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -3,129 +3,95 @@
33
*
44
* @author goofballLogic
55
*/
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

Comments
 (0)