From 5f0e8009bc85ee270fbd80961f3a7205b8e4f263 Mon Sep 17 00:00:00 2001 From: Jonathan Cardoso Machado Date: Sun, 8 Dec 2024 23:30:29 -0300 Subject: [PATCH] fix(tests): use vitest runIf to conditionally run tests based on libcurl version --- test/curl/callbacks.spec.ts | 18 +- test/curl/easy.spec.ts | 43 +-- test/curl/hsts.spec.ts | 529 ++++++++++++++++++------------------ test/curl/setOpt.spec.ts | 24 +- test/curl/streams.spec.ts | 109 ++++---- 5 files changed, 362 insertions(+), 361 deletions(-) diff --git a/test/curl/callbacks.spec.ts b/test/curl/callbacks.spec.ts index 9d8ad322..04a82c70 100644 --- a/test/curl/callbacks.spec.ts +++ b/test/curl/callbacks.spec.ts @@ -142,8 +142,9 @@ describe('Callbacks', () => { }, 10000) }, 10000) - if (Curl.isVersionGreaterOrEqualThan(7, 64, 0)) { - describe('trailer', function () { + describe.runIf(Curl.isVersionGreaterOrEqualThan(7, 64, 0))( + 'trailer', + function () { it('should work', async () => { let wasCalled = false let isFirstCall = true @@ -293,11 +294,12 @@ describe('Callbacks', () => { curl.perform() }) }) - }) - } + }, + ) - if (Curl.isVersionGreaterOrEqualThan(7, 80, 0)) { - describe('prereq', function () { + describe.runIf(Curl.isVersionGreaterOrEqualThan(7, 80, 0))( + 'prereq', + function () { it('should work', async () => { let wasCalled = false @@ -395,6 +397,6 @@ describe('Callbacks', () => { curl.perform() }) }) - }) - } + }, + ) }, 5000) diff --git a/test/curl/easy.spec.ts b/test/curl/easy.spec.ts index d2f79630..36767696 100644 --- a/test/curl/easy.spec.ts +++ b/test/curl/easy.spec.ts @@ -86,8 +86,9 @@ describe('easy', () => { ) }) - if (Curl.isVersionGreaterOrEqualThan(7, 64, 0)) { - it('TRAILERFUNCTION - should rethrow error', () => { + describe.runIf(Curl.isVersionGreaterOrEqualThan(7, 64, 0))( + 'TRAILERFUNCTION - should rethrow error', + () => { curl.setOpt('UPLOAD', true) curl.setOpt('HTTPHEADER', ['x-random-header: random-value']) // @ts-ignore @@ -103,27 +104,27 @@ describe('easy', () => { return buffer.write(data) }) expect(() => curl.perform()).toThrow('Error thrown on callback') - }) + }, + ) - it('TRAILERFUNCTION - should throw error if has invalid return type', () => { - curl.setOpt('UPLOAD', true) - curl.setOpt('HTTPHEADER', ['x-random-header: random-value']) - // @ts-ignore - curl.setOpt('TRAILERFUNCTION', () => { - return {} - }) - let finished = false - curl.setOpt(Curl.option.READFUNCTION, (buffer, _size, _nmemb) => { - if (finished) return 0 + it('TRAILERFUNCTION - should throw error if has invalid return type', () => { + curl.setOpt('UPLOAD', true) + curl.setOpt('HTTPHEADER', ['x-random-header: random-value']) + // @ts-ignore + curl.setOpt('TRAILERFUNCTION', () => { + return {} + }) + let finished = false + curl.setOpt(Curl.option.READFUNCTION, (buffer, _size, _nmemb) => { + if (finished) return 0 - const data = 'HELLO' - finished = true - return buffer.write(data) - }) - expect(() => curl.perform()).toThrow( - 'Return value from the Trailer callback must be an array of strings or false.', - ) + const data = 'HELLO' + finished = true + return buffer.write(data) }) - } + expect(() => curl.perform()).toThrow( + 'Return value from the Trailer callback must be an array of strings or false.', + ) + }) }) }) diff --git a/test/curl/hsts.spec.ts b/test/curl/hsts.spec.ts index 8ff782c0..f666d836 100644 --- a/test/curl/hsts.spec.ts +++ b/test/curl/hsts.spec.ts @@ -46,355 +46,354 @@ const getHstsCache = (): CurlHstsCacheEntry[] => [ }, ] -if (Curl.isVersionGreaterOrEqualThan(7, 74, 0)) { - describe('Callbacks', () => { - beforeEach(() => { - curl = new Curl() - curl.setOpt('URL', url) - if (process.version.startsWith('v10.')) { - curl.setOpt('SSL_VERIFYPEER', false) - } else { - curl.setOpt('CAINFO_BLOB', tls.rootCertificates.join('\n')) - } - }) +describe.runIf(Curl.isVersionGreaterOrEqualThan(7, 74, 0))('Callbacks', () => { + beforeEach(() => { + curl = new Curl() + curl.setOpt('URL', url) + if (process.version.startsWith('v10.')) { + curl.setOpt('SSL_VERIFYPEER', false) + } else { + curl.setOpt('CAINFO_BLOB', tls.rootCertificates.join('\n')) + } + }) + + afterEach(() => { + if (curl.isOpen) { + curl.close() + } + }) - afterEach(() => { - if (curl.isOpen) { - curl.close() - } + describe('hsts', function () { + it('HSTSREADFUNCTION should work if returning null', async () => { + curl.setOpt('HSTS_CTRL', CurlHsts.Enable) + let callCount = 0 + curl.setOpt('HSTSREADFUNCTION', () => { + callCount++ + return null + }) + + await new Promise((resolve, reject) => { + curl.on('end', () => { + if (callCount > 1) { + reject( + new Error( + 'HSTSREADFUNCTION was called more than once when returning null', + ), + ) + } else { + resolve() + } + }) + curl.on('error', reject) + curl.perform() + }) }) - describe('hsts', function () { - it('HSTSREADFUNCTION should work if returning null', async () => { - curl.setOpt('HSTS_CTRL', CurlHsts.Enable) - let callCount = 0 + // libcurl <= 7.79 has a bug where HSTSREADFUNCTION is always called if it is set, see: + // https://github.com/curl/curl/issues/7710 + it.runIf(Curl.isVersionGreaterOrEqualThan(7, 80, 0))( + 'HSTSREADFUNCTION should not be called if HSTS_CTRL is disabled', + async () => { + curl.setOpt('HSTS_CTRL', CurlHsts.Disabled) + let wasCalled = false curl.setOpt('HSTSREADFUNCTION', () => { - callCount++ + wasCalled = true return null }) await new Promise((resolve, reject) => { curl.on('end', () => { - if (callCount > 1) { + if (wasCalled) { reject( new Error( - 'HSTSREADFUNCTION was called more than once when returning null', + 'HSTSREADFUNCTION was called while HSTS_CTRL was set to CurlHsts.Disabled', ), ) } else { resolve() } }) + curl.on('error', reject) + curl.perform() }) - }) + }, + ) - // libcurl <= 7.79 has a bug where HSTSREADFUNCTION is always called if it is set, see: - // https://github.com/curl/curl/issues/7710 - if (Curl.isVersionGreaterOrEqualThan(7, 80, 0)) { - it('HSTSREADFUNCTION should not be called if HSTS_CTRL is disabled', async () => { - curl.setOpt('HSTS_CTRL', CurlHsts.Disabled) - let wasCalled = false - curl.setOpt('HSTSREADFUNCTION', () => { - wasCalled = true - return null - }) + it('HSTSREADFUNCTION should work correctly by returning object multiple times', async () => { + curl.setOpt('HSTS_CTRL', CurlHsts.Enable) + const cache = getHstsCache() + const initialCacheLength = cache.length + let hstsReadFunctionCallCount = 0 - await new Promise((resolve, reject) => { - curl.on('end', () => { - if (wasCalled) { - reject( - new Error( - 'HSTSREADFUNCTION was called while HSTS_CTRL was set to CurlHsts.Disabled', - ), - ) - } else { - resolve() - } - }) - - curl.on('error', reject) - - curl.perform() - }) - }) - } - - it('HSTSREADFUNCTION should work correctly by returning object multiple times', async () => { - curl.setOpt('HSTS_CTRL', CurlHsts.Enable) - const cache = getHstsCache() - const initialCacheLength = cache.length - let hstsReadFunctionCallCount = 0 + curl.setOpt('HSTSREADFUNCTION', () => { + hstsReadFunctionCallCount++ + const entry = cache.shift() - curl.setOpt('HSTSREADFUNCTION', () => { - hstsReadFunctionCallCount++ - const entry = cache.shift() + return entry ?? null + }) - return entry ?? null + await new Promise((resolve, reject) => { + curl.on('end', () => { + expect(cache).toHaveLength(0) + expect(hstsReadFunctionCallCount).toBe(initialCacheLength + 1) + resolve() }) - await new Promise((resolve, reject) => { - curl.on('end', () => { - expect(cache).toHaveLength(0) - expect(hstsReadFunctionCallCount).toBe(initialCacheLength + 1) - resolve() - }) - - curl.on('error', reject) + curl.on('error', reject) - curl.perform() - }) + curl.perform() }) + }) - it('HSTSREADFUNCTION should work correctly by returning an array a single time', async () => { - curl.setOpt('HSTS_CTRL', CurlHsts.Enable) - const cache = getHstsCache() + it('HSTSREADFUNCTION should work correctly by returning an array a single time', async () => { + curl.setOpt('HSTS_CTRL', CurlHsts.Enable) + const cache = getHstsCache() - const callsToHstsReadFunction: boolean[] = [] + const callsToHstsReadFunction: boolean[] = [] - // callback will be called 3 times, as when we call duphandle libcurl calls this automatically - curl.setOpt('HSTSREADFUNCTION', function () { - // @ts-expect-error .handle is protected - callsToHstsReadFunction.push(this === curl.handle) + // callback will be called 3 times, as when we call duphandle libcurl calls this automatically + curl.setOpt('HSTSREADFUNCTION', function () { + // @ts-expect-error .handle is protected + callsToHstsReadFunction.push(this === curl.handle) - return cache ?? null - }) - - await new Promise((resolve, reject) => { - curl.on('end', () => { - const dupHandle = curl.dupHandle(false) + return cache ?? null + }) - // kill the original just to make sure we are not relying on memory from it - curl.close() + await new Promise((resolve, reject) => { + curl.on('end', () => { + const dupHandle = curl.dupHandle(false) - dupHandle.on('end', () => { - // first two calls will be true, as it is realted for the first instance: - // 1: perform - // 2: duphandle - // the third call will be false, as the instance will not be === curl.handle, but === dupHandle.handle - expect(callsToHstsReadFunction).toEqual([true, true, false]) + // kill the original just to make sure we are not relying on memory from it + curl.close() - resolve() - }) - dupHandle.on('error', reject) + dupHandle.on('end', () => { + // first two calls will be true, as it is realted for the first instance: + // 1: perform + // 2: duphandle + // the third call will be false, as the instance will not be === curl.handle, but === dupHandle.handle + expect(callsToHstsReadFunction).toEqual([true, true, false]) - dupHandle.perform() + resolve() }) + dupHandle.on('error', reject) - curl.on('error', reject) - - curl.perform() + dupHandle.perform() }) - }) - it('HSTSWRITEFUNCTION should work correctly by returning the same number of items that were provided by HSTSREADFUNCTION', async () => { - curl.setOpt('HSTS_CTRL', CurlHsts.Enable) + curl.on('error', reject) - const originalHstsCache = getHstsCache() - const savedHstsCache = [] as CurlHstsCacheEntry[] - const savedCount = [] as CurlHstsCacheCount[] + curl.perform() + }) + }) - const callsToHstsReadFunction: boolean[] = [] + it('HSTSWRITEFUNCTION should work correctly by returning the same number of items that were provided by HSTSREADFUNCTION', async () => { + curl.setOpt('HSTS_CTRL', CurlHsts.Enable) - // callback will be called 3 times, as when we call duphandle libcurl calls this automatically - curl.setOpt('HSTSREADFUNCTION', function () { - // @ts-expect-error .handle is protected - callsToHstsReadFunction.push(this === curl.handle) - return originalHstsCache ?? null - }) + const originalHstsCache = getHstsCache() + const savedHstsCache = [] as CurlHstsCacheEntry[] + const savedCount = [] as CurlHstsCacheCount[] - // callback will be called once, when handle is closed - curl.setOpt('HSTSWRITEFUNCTION', function (data, count) { - savedHstsCache.push(data) - savedCount.push(count) - return CurlHstsCallback.Ok - }) + const callsToHstsReadFunction: boolean[] = [] - await new Promise((resolve, reject) => { - curl.on('end', () => { - // kill the handle so the cache is saved - this is a sync operation so everything will happen in a synchronous way - curl.close() - - expect(savedHstsCache).toHaveLength(originalHstsCache.length) - - // cache is already updated here - savedHstsCache.forEach((value, index) => { - const matchingHstsCache = originalHstsCache[index] - - expect(value.host).toBe(matchingHstsCache.host) - - // this one should be equal, as it should have been updated - if (value.host.indexOf('owasp') !== -1) { - expect(value.expire).toBeDefined() - expect(value.expire).not.toBe(matchingHstsCache.expire) - // should be false as this is what is returned from the domain - expect(value.includeSubDomains).toBe(true) - } else { - expect(value.expire).toBe(matchingHstsCache.expire || null) - expect(value.includeSubDomains).toBe( - matchingHstsCache.includeSubDomains || false, - ) - } - }) - - expect(savedCount).toEqual( - new Array(originalHstsCache.length) - .fill(null) - .map((_v, index, arr) => ({ - index, - total: arr.length, - })), - ) + // callback will be called 3 times, as when we call duphandle libcurl calls this automatically + curl.setOpt('HSTSREADFUNCTION', function () { + // @ts-expect-error .handle is protected + callsToHstsReadFunction.push(this === curl.handle) + return originalHstsCache ?? null + }) - resolve() - }) + // callback will be called once, when handle is closed + curl.setOpt('HSTSWRITEFUNCTION', function (data, count) { + savedHstsCache.push(data) + savedCount.push(count) + return CurlHstsCallback.Ok + }) - curl.on('error', reject) + await new Promise((resolve, reject) => { + curl.on('end', () => { + // kill the handle so the cache is saved - this is a sync operation so everything will happen in a synchronous way + curl.close() - curl.perform() - }) - }) + expect(savedHstsCache).toHaveLength(originalHstsCache.length) - it('returning invalid data from HSTSREADFUNCTION should throw an error', async () => { - curl.setOpt('HSTS_CTRL', CurlHsts.Enable) + // cache is already updated here + savedHstsCache.forEach((value, index) => { + const matchingHstsCache = originalHstsCache[index] - let hstsReadFunctionCallCount = 0 - let onErrorCallCount = 0 - - const values = [ - 'this-is-invalid', - {}, - { - host: 123, - }, - { - host: 'domain.com', - includeSubDomains: 'abc', - }, - { - host: 'domain.com', - expire: false, - }, - { - host: 'a'.repeat(1024), - }, - ] - - const initialValuesLength = values.length - - // @ts-expect-error this should give an error because the values we are returning are not the ones HSTSREADFUNCTION expects - curl.setOpt('HSTSREADFUNCTION', function () { - hstsReadFunctionCallCount++ - - return values.pop() ?? null - }) + expect(value.host).toBe(matchingHstsCache.host) - await new Promise((resolve, reject) => { - curl.on('end', () => { - if (onErrorCallCount !== initialValuesLength) { - reject( - new Error( - `End event emitted too soon - Errors: ${onErrorCallCount} - Expected: ${initialValuesLength}`, - ), - ) + // this one should be equal, as it should have been updated + if (value.host.indexOf('owasp') !== -1) { + expect(value.expire).toBeDefined() + expect(value.expire).not.toBe(matchingHstsCache.expire) + // should be false as this is what is returned from the domain + expect(value.includeSubDomains).toBe(true) } else { - resolve() + expect(value.expire).toBe(matchingHstsCache.expire || null) + expect(value.includeSubDomains).toBe( + matchingHstsCache.includeSubDomains || false, + ) } }) - curl.on('error', (error, errorCode) => { - ++onErrorCallCount + expect(savedCount).toEqual( + new Array(originalHstsCache.length) + .fill(null) + .map((_v, index, arr) => ({ + index, + total: arr.length, + })), + ) - if (onErrorCallCount > initialValuesLength) { - reject( - new Error( - `onError called more times than expected - Expected: ${initialValuesLength} - Error: ${error.message}`, - ), - ) - return - } + resolve() + }) - expect(error.message).toMatch(/fix the HSTS callback/) - expect(errorCode).toBe(CurlCode.CURLE_ABORTED_BY_CALLBACK) + curl.on('error', reject) - expect(hstsReadFunctionCallCount).toBe(onErrorCallCount) + curl.perform() + }) + }) - curl.perform() - }) + it('returning invalid data from HSTSREADFUNCTION should throw an error', async () => { + curl.setOpt('HSTS_CTRL', CurlHsts.Enable) + + let hstsReadFunctionCallCount = 0 + let onErrorCallCount = 0 + + const values = [ + 'this-is-invalid', + {}, + { + host: 123, + }, + { + host: 'domain.com', + includeSubDomains: 'abc', + }, + { + host: 'domain.com', + expire: false, + }, + { + host: 'a'.repeat(1024), + }, + ] + + const initialValuesLength = values.length + + // @ts-expect-error this should give an error because the values we are returning are not the ones HSTSREADFUNCTION expects + curl.setOpt('HSTSREADFUNCTION', function () { + hstsReadFunctionCallCount++ + + return values.pop() ?? null + }) - curl.perform() + await new Promise((resolve, reject) => { + curl.on('end', () => { + if (onErrorCallCount !== initialValuesLength) { + reject( + new Error( + `End event emitted too soon - Errors: ${onErrorCallCount} - Expected: ${initialValuesLength}`, + ), + ) + } else { + resolve() + } }) - }) - it('throwing an error from inside HSTSREADFUNCTION should work correctly', async () => { - curl.setOpt('HSTS_CTRL', CurlHsts.Enable) + curl.on('error', (error, errorCode) => { + ++onErrorCallCount - let hstsReadFunctionCallCount = 0 + if (onErrorCallCount > initialValuesLength) { + reject( + new Error( + `onError called more times than expected - Expected: ${initialValuesLength} - Error: ${error.message}`, + ), + ) + return + } - const errorMessage = 'Something went wrong' + expect(error.message).toMatch(/fix the HSTS callback/) + expect(errorCode).toBe(CurlCode.CURLE_ABORTED_BY_CALLBACK) - curl.setOpt('HSTSREADFUNCTION', function () { - if (hstsReadFunctionCallCount++ === 0) { - throw new Error(errorMessage) - } + expect(hstsReadFunctionCallCount).toBe(onErrorCallCount) - // this should never get here as the cb should not be called again if we find an error - return null + curl.perform() }) - await new Promise((resolve, reject) => { - curl.on('end', () => { - reject(new Error('No error found - end was called')) - }) + curl.perform() + }) + }) - curl.on('error', (error, errorCode) => { - expect(error.message).toBe(errorMessage) - expect(errorCode).toBe(CurlCode.CURLE_ABORTED_BY_CALLBACK) + it('throwing an error from inside HSTSREADFUNCTION should work correctly', async () => { + curl.setOpt('HSTS_CTRL', CurlHsts.Enable) - expect(hstsReadFunctionCallCount).toBe(1) + let hstsReadFunctionCallCount = 0 - resolve() - }) + const errorMessage = 'Something went wrong' - curl.perform() - }) - }) + curl.setOpt('HSTSREADFUNCTION', function () { + if (hstsReadFunctionCallCount++ === 0) { + throw new Error(errorMessage) + } - describe('Easy', () => { - let easy: Easy + // this should never get here as the cb should not be called again if we find an error + return null + }) - beforeEach(() => { - easy = new Easy() - easy.setOpt('URL', url) + await new Promise((resolve, reject) => { + curl.on('end', () => { + reject(new Error('No error found - end was called')) }) - afterEach(() => { - if (easy.isOpen) { - easy.close() - } + curl.on('error', (error, errorCode) => { + expect(error.message).toBe(errorMessage) + expect(errorCode).toBe(CurlCode.CURLE_ABORTED_BY_CALLBACK) + + expect(hstsReadFunctionCallCount).toBe(1) + + resolve() }) - it('throwing an error from inside HSTSREADFUNCTION should work correctly', () => { - easy.setOpt('HSTS_CTRL', CurlHsts.Enable) + curl.perform() + }) + }) + + describe('Easy', () => { + let easy: Easy - let hstsReadFunctionCallCount = 0 + beforeEach(() => { + easy = new Easy() + easy.setOpt('URL', url) + }) - const errorMessage = 'Something went wrong' + afterEach(() => { + if (easy.isOpen) { + easy.close() + } + }) - easy.setOpt('HSTSREADFUNCTION', function () { - if (hstsReadFunctionCallCount++ === 0) { - throw new TypeError(errorMessage) - } + it('throwing an error from inside HSTSREADFUNCTION should work correctly', () => { + easy.setOpt('HSTS_CTRL', CurlHsts.Enable) - // this should never get here as the cb should not be called again if we find an error - return null - }) + let hstsReadFunctionCallCount = 0 - expect(() => easy.perform()).toThrow(errorMessage) - expect(hstsReadFunctionCallCount).toBe(1) + const errorMessage = 'Something went wrong' + + easy.setOpt('HSTSREADFUNCTION', function () { + if (hstsReadFunctionCallCount++ === 0) { + throw new TypeError(errorMessage) + } + + // this should never get here as the cb should not be called again if we find an error + return null }) + + expect(() => easy.perform()).toThrow(errorMessage) + expect(hstsReadFunctionCallCount).toBe(1) }) }) }) -} +}) diff --git a/test/curl/setOpt.spec.ts b/test/curl/setOpt.spec.ts index 52c0b0de..f01c2d25 100644 --- a/test/curl/setOpt.spec.ts +++ b/test/curl/setOpt.spec.ts @@ -199,20 +199,18 @@ describe('setOpt()', () => { }) }) - if (Curl.isVersionGreaterOrEqualThan(7, 71, 0)) { - describe('BLOB', () => { - it('should be able to set blob value back to null', () => { - curl.setOpt('SSLKEY_BLOB', Buffer.from([])) - curl.setOpt('SSLKEY_BLOB', null) - }) + describe.runIf(Curl.isVersionGreaterOrEqualThan(7, 71, 0))('BLOB', () => { + it('should be able to set blob value back to null', () => { + curl.setOpt('SSLKEY_BLOB', Buffer.from([])) + curl.setOpt('SSLKEY_BLOB', null) + }) - it('should be able to set blob value to buffer', () => { - curl.setOpt('SSLKEY_BLOB', Buffer.from(pemFormattedPrivateKey, 'utf-8')) - }) + it('should be able to set blob value to buffer', () => { + curl.setOpt('SSLKEY_BLOB', Buffer.from(pemFormattedPrivateKey, 'utf-8')) + }) - it('should be able to set blob value to string', () => { - curl.setOpt('SSLKEY_BLOB', pemFormattedPrivateKey) - }) + it('should be able to set blob value to string', () => { + curl.setOpt('SSLKEY_BLOB', pemFormattedPrivateKey) }) - } + }) }) diff --git a/test/curl/streams.spec.ts b/test/curl/streams.spec.ts index 066c74f5..012472d5 100644 --- a/test/curl/streams.spec.ts +++ b/test/curl/streams.spec.ts @@ -118,8 +118,9 @@ describe('streams', () => { describe('curly', () => { // libcurl versions older than this are not really reliable for streams usage. - if (Curl.isVersionGreaterOrEqualThan(7, 69, 1)) { - it('works for uploading and downloading', async () => { + describe.runIf(Curl.isVersionGreaterOrEqualThan(7, 69, 1))( + 'works for uploading and downloading', + async () => { const curlyStreamUpload = getReadableStreamForBuffer(randomBuffer, { filterDataToPush: async (pushIteration, data) => { // we are waiting 1200 ms at the 5th iteration just to cause @@ -184,80 +185,80 @@ describe('streams', () => { reject(error) }) }) - }) + }, + ) - it('works with responses without body', async () => { - const { statusCode, data: downloadStream } = await curly.get( - `${serverInstance.url}/all?type=no-body`, - { - ...getDownloadOptions(), - curlyProgressCallback() { - return 0 - }, + it('works with responses without body', async () => { + const { statusCode, data: downloadStream } = await curly.get( + `${serverInstance.url}/all?type=no-body`, + { + ...getDownloadOptions(), + curlyProgressCallback() { + return 0 }, - ) + }, + ) - expect(statusCode).toBe(200) + expect(statusCode).toBe(200) - // TODO: add snapshot testing for headers + // TODO: add snapshot testing for headers - // we cannot use async iterators here because we need to support Node.js v8 + // we cannot use async iterators here because we need to support Node.js v8 - return new Promise((resolve, reject) => { - const acc: Buffer[] = [] + return new Promise((resolve, reject) => { + const acc: Buffer[] = [] - downloadStream.on('data', (data) => { - acc.push(data) - }) + downloadStream.on('data', (data) => { + acc.push(data) + }) - downloadStream.on('end', () => { - const finalBuffer = Buffer.concat(acc) - expect(finalBuffer.byteLength).toBe(0) - resolve(undefined) - }) + downloadStream.on('end', () => { + const finalBuffer = Buffer.concat(acc) + expect(finalBuffer.byteLength).toBe(0) + resolve(undefined) + }) - downloadStream.on('error', (error) => { - reject(error) - }) + downloadStream.on('error', (error) => { + reject(error) }) }) + }) - it('works with HEAD requests', async () => { - const { statusCode, data: downloadStream } = await curly.head( - `${serverInstance.url}/all?type=method`, - { - ...getDownloadOptions(), - curlyProgressCallback() { - return 0 - }, + it('works with HEAD requests', async () => { + const { statusCode, data: downloadStream } = await curly.head( + `${serverInstance.url}/all?type=method`, + { + ...getDownloadOptions(), + curlyProgressCallback() { + return 0 }, - ) + }, + ) - expect(statusCode).toBe(200) + expect(statusCode).toBe(200) - // TODO: add snapshot testing for headers + // TODO: add snapshot testing for headers - // we cannot use async iterators here because we need to support Node.js v8 + // we cannot use async iterators here because we need to support Node.js v8 - return new Promise((resolve, reject) => { - const acc: Buffer[] = [] + return new Promise((resolve, reject) => { + const acc: Buffer[] = [] - downloadStream.on('data', (data) => { - acc.push(data) - }) + downloadStream.on('data', (data) => { + acc.push(data) + }) - downloadStream.on('end', () => { - const finalBuffer = Buffer.concat(acc) - expect(finalBuffer.byteLength).toBe(0) - resolve(undefined) - }) + downloadStream.on('end', () => { + const finalBuffer = Buffer.concat(acc) + expect(finalBuffer.byteLength).toBe(0) + resolve(undefined) + }) - downloadStream.on('error', (error) => { - reject(error) - }) + downloadStream.on('error', (error) => { + reject(error) }) }) - } + }) it('returns an error when the upload stream throws an error', async () => { const errorMessage = 'custom error'