Skip to content

Commit

Permalink
Merge 8673780 into 3d18e9f
Browse files Browse the repository at this point in the history
  • Loading branch information
csbun committed Jan 30, 2018
2 parents 3d18e9f + 8673780 commit c1188f1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
12 changes: 10 additions & 2 deletions index.js
Expand Up @@ -52,7 +52,7 @@ module.exports = async function (ctx, next) {
}
}

//Adjust infinite end
// Adjust infinite end
if (end === Infinity) {
if (Number.isInteger(len)) {
end = len - 1;
Expand All @@ -63,12 +63,20 @@ module.exports = async function (ctx, next) {
}
}

ctx.status = 206;
// Adjust end while larger than len
if (Number.isInteger(len) && end >= len) {
end = len - 1;
if (start === 0) {
ctx.status = 200;
}
}

var args = [start, end+1].filter(function(item) {
return typeof item == 'number';
});

ctx.set('Content-Range', rangeContentGenerator(start, end, len));
ctx.status = 206;

if (rawBody instanceof Stream) {
ctx.body = rawBody;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -33,7 +33,7 @@
"dependencies": {
"stream-slice": "^0.1.2"
},
"engines" : {
"node" : ">=7"
"engines": {
"node": ">=7"
}
}
22 changes: 22 additions & 0 deletions test/simple.js
Expand Up @@ -69,6 +69,28 @@ describe('range requests', function() {
.end(done);
});

it('should return 200 when using oversized end', function(done) {
request(app.listen())
.get('/')
.set('range', 'bytes=0-1024') // 1025 bytes in total > 1024
.expect('Content-Length', '1024')
.expect('Accept-Ranges', 'bytes')
.expect('Content-Range', 'bytes 0-1023/1024')
.expect(200)
.end(done);
});

it('should return 206 with partial content when using oversized end', function(done) {
request(app.listen())
.get('/')
.set('range', 'bytes=1000-1024')
.expect('Content-Length', '24')
.expect('Accept-Ranges', 'bytes')
.expect('Content-Range', 'bytes 1000-1023/1024')
.expect(206)
.end(done);
});

it('should return 400 with PUT', function(done) {
request(app.listen())
.put('/')
Expand Down

0 comments on commit c1188f1

Please sign in to comment.