Skip to content
22 changes: 18 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,18 @@ function rangeParser (size, str, options) {
// add ranges type
ranges.type = str.slice(0, index)

// check if there are multiple ranges
var isMultiple = arr.length > 1
var status = -2 // -2: all ranges invalid, -1: at least one valid but unsatisfiable

// parse all ranges
for (var i = 0; i < arr.length; i++) {
var indexOf = arr[i].indexOf('-')
if (indexOf === -1) {
// ignore empty/whitespace-only ranges if there are other valid ranges
if (arr.length > 1 && arr[i].trim() === '') {
continue
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This handles a case where the range can be 'bytes= , 0-10', which before the two PRs was valid and returned ranges, but without this patch it returns -2.

return -2
}

Expand All @@ -67,11 +75,18 @@ function rangeParser (size, str, options) {
end = size - 1
}

// invalid format range
if (isNaN(start) || isNaN(end)) {
return -2
if (!isMultiple) {
return -2
}
continue
}

// invalid or unsatisifiable
// unsatisifiable
status = -1

// skip unsatisfiable ranges
if (start > end || start < 0) {
continue
}
Expand All @@ -84,8 +99,7 @@ function rangeParser (size, str, options) {
}

if (ranges.length < 1) {
// unsatisifiable
return -1
return status
}

return options && options.combine
Expand Down
57 changes: 56 additions & 1 deletion test/range-parser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

var assert = require('assert')
var deepEqual = require('deep-equal')
var parse = require('..')
Expand All @@ -9,6 +8,16 @@ describe('parseRange(len, str)', function () {
/TypeError: argument str must be a string/)
})

it('should return -2 for completely empty header', function () {
assert.strictEqual(parse(200, ''), -2)
})

it('should return -2 for range missing dash', function () {
assert.strictEqual(parse(200, 'bytes=100200'), -2)
assert.strictEqual(parse(200, 'bytes=,100200'), -2)
assert.strictEqual(parse(200, 'bytes=100-200,100200'), -2)
})
Comment on lines 15 to 18
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to get the coverage to 100%, it has nothing to do with my changes


it('should return -2 for invalid str', function () {
assert.strictEqual(parse(200, 'malformed'), -2)
})
Expand All @@ -28,6 +37,12 @@ describe('parseRange(len, str)', function () {
assert.strictEqual(parse(200, 'bytes= - '), -2)
})

it('should return -2 for empty range value', function () {
assert.strictEqual(parse(200, 'bytes='), -2)
assert.strictEqual(parse(200, 'bytes=,'), -2)
assert.strictEqual(parse(200, 'bytes= , , '), -2)
})

it('should return -2 with multiple dashes in range', function () {
assert.strictEqual(parse(200, 'bytes=100-200-300'), -2)
})
Expand All @@ -41,6 +56,12 @@ describe('parseRange(len, str)', function () {
assert.strictEqual(parse(200, 'bytes=100-15b0'), -2)
})

it('should return -2 when all multiple ranges have invalid format', function () {
assert.strictEqual(parse(200, 'bytes=y-v,x-'), -2)
assert.strictEqual(parse(200, 'bytes=abc-def,ghi-jkl'), -2)
assert.strictEqual(parse(200, 'bytes=x-,y-,z-'), -2)
})

it('should return -1 for unsatisfiable range', function () {
assert.strictEqual(parse(200, 'bytes=500-600'), -1)
})
Expand All @@ -55,6 +76,11 @@ describe('parseRange(len, str)', function () {
assert.strictEqual(parse(200, 'bytes=500-999,1000-1499'), -1)
})

it('should return -1 when invalid format mixed with unsatisfiable ranges', function () {
assert.strictEqual(parse(200, 'bytes=500-600,x-'), -1)
assert.strictEqual(parse(200, 'bytes=x-,500-600'), -1)
})

it('should parse str', function () {
var range = parse(1000, 'bytes=0-499')
assert.strictEqual(range.type, 'bytes')
Expand Down Expand Up @@ -111,6 +137,28 @@ describe('parseRange(len, str)', function () {
deepEqual(range[0], { start: 999, end: 999 })
})

it('should ignore invalid format range when valid range exists', function () {
var range = parse(1000, 'bytes=100-200,x-')
assert.strictEqual(range.type, 'bytes')
assert.strictEqual(range.length, 1)
deepEqual(range[0], { start: 100, end: 200 })
})

it('should ignore invalid format ranges when some are valid', function () {
var range = parse(1000, 'bytes=x-,0-100,y-')
assert.strictEqual(range.type, 'bytes')
assert.strictEqual(range.length, 1)
deepEqual(range[0], { start: 0, end: 100 })
})

it('should ignore invalid format ranges at different positions', function () {
var range = parse(1000, 'bytes=0-50,abc-def,100-150')
assert.strictEqual(range.type, 'bytes')
assert.strictEqual(range.length, 2)
deepEqual(range[0], { start: 0, end: 50 })
deepEqual(range[1], { start: 100, end: 150 })
})

it('should parse str with multiple ranges', function () {
var range = parse(1000, 'bytes=40-80,81-90,-1')
assert.strictEqual(range.type, 'bytes')
Expand Down Expand Up @@ -161,4 +209,11 @@ describe('parseRange(len, str)', function () {
deepEqual(range[2], { start: 0, end: 1 })
})
})

it('should ignore whitespace-only invalid ranges when valid present', function () {
var range = parse(1000, 'bytes= , 0-10')
assert.strictEqual(range.type, 'bytes')
assert.strictEqual(range.length, 1)
deepEqual(range[0], { start: 0, end: 10 })
})
})
Loading