Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: node 16 compatibility #2199

Merged
merged 4 commits into from May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yaml
Expand Up @@ -153,5 +153,5 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [10, 12, 14]
node-version: [10, 12, 14, 16]
os: [macos-latest, ubuntu-latest, windows-latest]
1 change: 1 addition & 0 deletions lib/socket.js
Expand Up @@ -92,6 +92,7 @@ module.exports = class Socket extends EventEmitter {
debug('socket destroy')
this.destroyed = true
this.readable = this.writable = false
this.readableEnded = this.writableFinished = true

process.nextTick(() => {
if (err) {
Expand Down
7 changes: 6 additions & 1 deletion tests/test_common.js
Expand Up @@ -14,6 +14,7 @@
const http = require('http')
const { expect } = require('chai')
const sinon = require('sinon')
const semver = require('semver')
const nock = require('..')

const common = require('../lib/common')
Expand Down Expand Up @@ -472,8 +473,12 @@ it('`percentEncode()` encodes extra reserved characters', () => {

describe('`normalizeClientRequestArgs()`', () => {
it('should throw for invalid URL', () => {
// See https://github.com/nodejs/node/pull/38614 release in node v16.2.0
const isNewErrorText = semver.gte(process.versions.node, '16.2.0')
const errorText = isNewErrorText ? 'Invalid URL' : 'example.test'

// no schema
expect(() => http.get('example.test')).to.throw(TypeError, 'example.test')
expect(() => http.get('example.test')).to.throw(TypeError, errorText)
})

it('can include auth info', async () => {
Expand Down
46 changes: 46 additions & 0 deletions tests/test_socket.js
Expand Up @@ -3,6 +3,7 @@
const { expect } = require('chai')
const http = require('http')
const https = require('https')
const { Readable } = require('stream')
const nock = require('..')

it('should expose TLSSocket attributes for HTTPS requests', done => {
Expand Down Expand Up @@ -50,3 +51,48 @@ describe('`Socket#setTimeout()`', () => {
})
})
})

describe('`Socket#destroy()`', () => {
it('can destroy the socket if stream is not finished', async () => {
const scope = nock('http://example.test')

scope.intercept('/somepath', 'GET').reply(() => {
const buffer = Buffer.allocUnsafe(10000000)
const data = new MemoryReadableStream(buffer, { highWaterMark: 128 })
return [200, data]
})

const req = http.get('http://example.test/somepath')
const stream = await new Promise(resolve => req.on('response', resolve))

// close after first chunk of data
stream.on('data', () => stream.destroy())

await new Promise((resolve, reject) => {
stream.on('error', reject)
stream.on('close', resolve)
stream.on('end', resolve)
})
})
})

class MemoryReadableStream extends Readable {
constructor(content) {
super()
this._content = content
this._currentOffset = 0
}

_read(size) {
if (this._currentOffset >= this._content.length) {
this.push(null)
return
}

const nextOffset = this._currentOffset + size
const content = this._content.slice(this._currentOffset, nextOffset)
this._currentOffset = nextOffset

this.push(content)
}
}