Skip to content

Commit

Permalink
Make replyContentLength to work on strings and buffers (#2294)
Browse files Browse the repository at this point in the history
Co-authored-by: Stefano Azzalini <stefano.azzalini@luminator.com>
  • Loading branch information
mastermatt and stfnzl committed Feb 2, 2022
1 parent c9a301c commit 79ee042
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/interceptor.js
Expand Up @@ -156,7 +156,7 @@ module.exports = class Interceptor {
)

// If the content is not encoded we may need to transform the response body.
// Otherwise we leave it as it is.
// Otherwise, we leave it as it is.
if (
body &&
typeof body !== 'string' &&
Expand All @@ -174,10 +174,14 @@ module.exports = class Interceptor {
// https://tools.ietf.org/html/rfc7231#section-3.1.1.5
this.rawHeaders.push('Content-Type', 'application/json')
}
}

if (this.scope.contentLen) {
// https://tools.ietf.org/html/rfc7230#section-3.3.2
if (this.scope.contentLen) {
// https://tools.ietf.org/html/rfc7230#section-3.3.2
if (typeof body === 'string') {
this.rawHeaders.push('Content-Length', body.length)
} else if (Buffer.isBuffer(body)) {
this.rawHeaders.push('Content-Length', body.byteLength)
}
}

Expand Down
45 changes: 45 additions & 0 deletions tests/test_reply_headers.js
Expand Up @@ -7,9 +7,14 @@ const { IncomingMessage } = require('http')
const { expect } = require('chai')
const sinon = require('sinon')
const fakeTimers = require('@sinonjs/fake-timers')
const fs = require('fs')
const path = require('path')

const nock = require('..')
const got = require('./got_client')

const textFilePath = path.resolve(__dirname, './assets/reply_file_1.txt')

describe('`reply()` headers', () => {
describe('using parameter value', () => {
it('as array', async () => {
Expand Down Expand Up @@ -331,6 +336,46 @@ describe('`replyContentLength()`', () => {
)
scope.done()
})

it('sends explicit content-length header with string response', async () => {
const response = '<html><body>...</body></html>'

const scope = nock('http://example.test')
.replyContentLength()
.get('/')
.reply(200, response)

const { headers } = await got('http://example.test/')

expect(headers['content-length']).to.equal(`${response.length}`)
scope.done()
})

it('sends explicit content-length header with buffer response', async () => {
const response = Buffer.from([1, 2, 3, 4, 5, 6])

const scope = nock('http://example.test')
.replyContentLength()
.get('/')
.reply(200, response)

const { headers } = await got('http://example.test/')

expect(headers['content-length']).to.equal(`${response.byteLength}`)
scope.done()
})

it('should not send content-length when responding with a stream', async () => {
const scope = nock('http://example.test')
.replyContentLength()
.get('/')
.reply(200, () => fs.createReadStream(textFilePath))

const { headers } = await got('http://example.test/')

expect(headers['content-length']).to.be.undefined()
scope.done()
})
})

describe('`replyDate()`', () => {
Expand Down

0 comments on commit 79ee042

Please sign in to comment.