diff --git a/index.js b/index.js index 1d09e26..41e1cd3 100644 --- a/index.js +++ b/index.js @@ -38,6 +38,7 @@ function rangeParser (size, str, options) { // split the range string var arr = str.slice(index + 1).split(',') var ranges = [] + var valid = false // add ranges type ranges.type = str.slice(0, index) @@ -46,7 +47,7 @@ function rangeParser (size, str, options) { for (var i = 0; i < arr.length; i++) { var indexOf = arr[i].indexOf('-') if (indexOf === -1) { - return -2 + continue } var startStr = arr[i].slice(0, indexOf).trim() @@ -67,12 +68,14 @@ function rangeParser (size, str, options) { end = size - 1 } + // invalid format range if (isNaN(start) || isNaN(end)) { - return -2 + continue } - // invalid or unsatisifiable + // skip unsatisfiable ranges if (start > end || start < 0) { + valid = true continue } @@ -84,8 +87,7 @@ function rangeParser (size, str, options) { } if (ranges.length < 1) { - // unsatisifiable - return -1 + return valid ? -1 : -2 } return options && options.combine diff --git a/test/range-parser.js b/test/range-parser.js index f882a93..9f2c483 100644 --- a/test/range-parser.js +++ b/test/range-parser.js @@ -1,4 +1,3 @@ - var assert = require('assert') var deepEqual = require('deep-equal') var parse = require('..') @@ -9,6 +8,15 @@ 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) + }) + it('should return -2 for invalid str', function () { assert.strictEqual(parse(200, 'malformed'), -2) }) @@ -28,6 +36,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) }) @@ -41,6 +55,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) }) @@ -55,6 +75,12 @@ describe('parseRange(len, str)', function () { assert.strictEqual(parse(200, 'bytes=500-999,1000-1499'), -1) }) + it('should return -1 for mixed invalid and unsatisfiable ranges', function () { + assert.strictEqual(parse(200, 'bytes=abc-def,500-999'), -1) + assert.strictEqual(parse(200, 'bytes=500-999,xyz-uvw'), -1) + assert.strictEqual(parse(200, 'bytes=abc-def,500-999,xyz-uvw'), -1) + }) + it('should parse str', function () { var range = parse(1000, 'bytes=0-499') assert.strictEqual(range.type, 'bytes') @@ -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') @@ -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 }) + }) })