-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHTTPHeaderUtilTest.js
More file actions
52 lines (40 loc) · 2.26 KB
/
HTTPHeaderUtilTest.js
File metadata and controls
52 lines (40 loc) · 2.26 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
var assert = require('assert');
var format = require('util').format;
var headerUtil = require( 'RESThttp' ).httpHeaderUtil;
var check = {
rangeFormat : 'bytes %d-%d/%d',
start : 647567,
end : 8374656,
length : 7727090,
size : 98675857
}
var headerWithStartEndRange = {
'range' : 'bytes=' + check.start + '-' + check.end
}
var headerWithStartOnlyRange = {
'range' : 'bytes=' + check.start + '-'
}
var headerWithEndOnlyRange = {
'range' : 'bytes=-' + check.end
}
assert.ok( headerUtil.hasRange( headerWithStartEndRange ), 'hasRange should be true with rangeHeader' );
assert.equal( headerUtil.hasRange( {} ), false, 'hasRange should be false with noRangeHeader' );
assert.deepEqual( headerUtil.extractRange( headerWithStartEndRange ), { start : check.start, end : check.end, length : check.length }, 'range should have start, end and length');
assert.deepEqual( headerUtil.extractRange( headerWithStartOnlyRange ), { start : check.start, end : undefined, length : undefined }, 'range should have start and undefined end and length');
assert.deepEqual( headerUtil.extractRange( headerWithStartOnlyRange, check.size ), { start : check.start, end : check.size - 1, length : check.size - check.start }, 'range should have start and undefined end and length');
assert.deepEqual( headerUtil.extractRange( headerWithEndOnlyRange ), { start : 0, end : check.end, length : check.end + 1 }, 'range should have start, end and length');
var rangeWithEnd = {
start : check.start,
end : check.end,
length : check.length
}
var rangeWithoutEnd = {
start : check.start,
end : undefined,
length : undefined
}
assert.equal( headerUtil.calcContentLength( rangeWithEnd, check.size ), check.end - check.start + 1, 'contentLength should be.' );
assert.equal( headerUtil.calcContentLength( rangeWithoutEnd, check.size ), check.size - check.start, 'contentLength should be.' );
assert.equal( headerUtil.createContentRangeString( rangeWithEnd, check.size ), format( check.rangeFormat, check.start, check.end, check.size ), 'content range with start and end.');
assert.equal( headerUtil.createContentRangeString( rangeWithoutEnd, check.size ), format( check.rangeFormat, check.start, check.size - 1, check.size ), 'content range with start only.');
console.log( 'HTTPHeaderUtilTest successful.' );