-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathbuildHeaders.test.js
More file actions
103 lines (91 loc) · 3.05 KB
/
buildHeaders.test.js
File metadata and controls
103 lines (91 loc) · 3.05 KB
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
101
102
103
var jsonld = require('../js/jsonld');
var assert = require('assert');
describe('Build a valid header object from pre-defined headers', function() {
describe('When built with 2 accept headers', function() {
var headers = {
'Accept': 'application/json',
'accept': 'application/ld+json, application/json'
};
it('should fail', function() {
assert.throws(
jsonld.buildHeaders.bind(null, headers),
function(err) {
assert.ok(err instanceof RangeError, 'A range error should be thrown');
assert.equal(err.message, 'Accept header may be specified only once.');
return true;
}
);
});
});
describe('When built with no explicit headers', function() {
var headers = {};
it(
'the "Accept" header should default to "application/ld+json, application/json"',
function() {
var actualHeaders = jsonld.buildHeaders(headers);
assert.deepEqual(actualHeaders, {
'Accept': 'application/ld+json, application/json'
});
}
);
});
describe('When built with custom headers', function() {
var headers = {
'Authorization': 'Bearer d783jkjaods9f87o83hj'
};
it('the custom headers should be preserved', function() {
var actualHeaders = jsonld.buildHeaders(headers);
assert.deepEqual(actualHeaders, {
'Authorization': 'Bearer d783jkjaods9f87o83hj',
'Accept': 'application/ld+json, application/json'
});
});
});
describe('When built with an accept header equal to the default accept header value', function() {
var headers = {
'Accept': 'application/ld+json, application/json'
};
it(
'the accept header should remain unchanged',
function() {
var actualHeaders = jsonld.buildHeaders(headers);
assert.deepEqual(actualHeaders, headers);
}
);
});
describe('When built with a valid accept headers with extra acceptations', function() {
var headers = {
'Accept': 'application/ld+json, application/json, text/html'
};
it(
'the accept header should remain unchanged',
function() {
var actualHeaders = jsonld.buildHeaders(headers);
assert.deepEqual(actualHeaders, headers);
}
);
});
describe('When built with an invalid accept header', function() {
var possibleInvalidHeaders = [
{ 'Accept': 'text/html' },
{ 'Accept': 'application/ld+json, application/jsonbourne' },
{ 'Accept': 'application/ld+json application/json' }
];
for (var i in possibleInvalidHeaders) {
console.log(possibleInvalidHeaders[i]);
it('should fail', function() {
assert.throws(
jsonld.buildHeaders.bind(null, possibleInvalidHeaders[i]),
function(err) {
assert.ok(err instanceof RangeError, 'A range error should be thrown');
assert.equal(
err.message,
'Accept header must contains "application/ld+json, application/json".'
);
return true;
}
);
});
}
});
});