Skip to content

Commit

Permalink
Updated size calculations to contain downloadedBytes and uncompressed…
Browse files Browse the repository at this point in the history
… bytes
  • Loading branch information
parthverma1 committed Jun 25, 2024
1 parent 8dfa17c commit ab93461
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
16 changes: 8 additions & 8 deletions lib/collection/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,13 +428,8 @@ _.assign(Response.prototype, /** @lends Response.prototype */ {
isCompressed = (contentEncoding.indexOf('gzip') > -1) || (contentEncoding.indexOf('deflate') > -1);
}

// if 'Content-Length' header is present and encoding is of type gzip/deflate, we take body as declared by
// server. else we need to compute the same.
if (contentLength && isCompressed && util.isNumeric(contentLength)) {
sizeInfo.body = _.parseInt(contentLength, 10);
}
// if there is a stream defined which looks like buffer, use it's data and move on
else if (this.stream) {
// If there is a stream defined which looks like buffer, use it's data to calculate the body
if (this.stream) {
byteLength = this.stream.byteLength;
sizeInfo.body = util.isNumeric(byteLength) ? byteLength :
/* istanbul ignore next */
Expand All @@ -448,7 +443,12 @@ _.assign(Response.prototype, /** @lends Response.prototype */ {
}

if (!sizeInfo.downloadedBytes) {
sizeInfo.downloadedBytes = sizeInfo.body;
if (contentLength && isCompressed && util.isNumeric(contentLength)) {
sizeInfo.downloadedBytes = _.parseInt(contentLength, 10);
}
else {
sizeInfo.downloadedBytes = sizeInfo.body;
}
}

// size of header is added
Expand Down
20 changes: 16 additions & 4 deletions test/unit/response.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,8 @@ describe('Response', function () {
size1 = response1.size(),
size2 = response2.size();

expect(size1.body + size1.header).to.eql(rawResponse1.header.length + rawResponse1.body.length);
expect(size2.body + size2.header).to.eql(rawResponse1.header.length + rawResponse1.body.length);
expect(size1.downloadedBytes + size1.header).to.eql(rawResponse1.header.length + rawResponse1.body.length);
expect(size2.downloadedBytes + size2.header).to.eql(rawResponse1.header.length + rawResponse1.body.length);
});

it('must match the content-length of the response if gzip encoded', function () {
Expand All @@ -471,7 +471,7 @@ describe('Response', function () {
},
response = new Response(rawResponse);

expect(response.size().body).to.equal(10);
expect(response.size().downloadedBytes).to.equal(10);
});

it('must match the content-length of the response if deflate encoded', function () {
Expand All @@ -482,7 +482,7 @@ describe('Response', function () {
},
response = new Response(rawResponse);

expect(response.size().body).to.equal(20);
expect(response.size().downloadedBytes).to.equal(20);
});

it('must use byteLength from buffer if provided', function () {
Expand All @@ -495,6 +495,18 @@ describe('Response', function () {

expect(response.size().body).to.equal(14);
});

it('must use body size if no downloaded bytes are provided', function () {
var rawResponse = {
code: 200,
body: 'something nice',
header: 'Transfer-Encoding: chunked'
},
response = new Response(rawResponse);

expect(response.size().downloadedBytes).to.equal(14);
expect(response.size().body).to.equal(14);
});
});

describe('toJSON', function () {
Expand Down

0 comments on commit ab93461

Please sign in to comment.