From ab934616934de6593f1fa98a4a79520c24b2b2c7 Mon Sep 17 00:00:00 2001 From: Parth Verma Date: Tue, 25 Jun 2024 14:59:01 -0700 Subject: [PATCH] Updated size calculations to contain downloadedBytes and uncompressed bytes --- lib/collection/response.js | 16 ++++++++-------- test/unit/response.test.js | 20 ++++++++++++++++---- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/lib/collection/response.js b/lib/collection/response.js index f7b641c77..947cad104 100644 --- a/lib/collection/response.js +++ b/lib/collection/response.js @@ -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 */ @@ -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 diff --git a/test/unit/response.test.js b/test/unit/response.test.js index c87ab00ec..03280f57e 100644 --- a/test/unit/response.test.js +++ b/test/unit/response.test.js @@ -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 () { @@ -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 () { @@ -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 () { @@ -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 () {