From d3dc77716dd4a665204183f577ccb63e453ae401 Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Thu, 21 Mar 2024 13:57:52 -0700 Subject: [PATCH] chore: move test fixtures --- .../test/abort-handling.spec.ts | 33 ++----------------- .../test/cache-control-header.spec.ts | 12 +------ .../test/fixtures/get-abortable-promise.ts | 17 ++++++++++ .../test/fixtures/make-aborted-request.ts | 14 ++++++++ 4 files changed, 34 insertions(+), 42 deletions(-) create mode 100644 packages/verified-fetch/test/fixtures/get-abortable-promise.ts create mode 100644 packages/verified-fetch/test/fixtures/make-aborted-request.ts diff --git a/packages/verified-fetch/test/abort-handling.spec.ts b/packages/verified-fetch/test/abort-handling.spec.ts index be1b26d2..54daa0ef 100644 --- a/packages/verified-fetch/test/abort-handling.spec.ts +++ b/packages/verified-fetch/test/abort-handling.spec.ts @@ -10,39 +10,10 @@ import Sinon from 'sinon' import { stubInterface, type StubbedInstance } from 'sinon-ts' import { VerifiedFetch } from '../src/verified-fetch.js' import { createHelia } from './fixtures/create-offline-helia.js' +import { getAbortablePromise } from './fixtures/get-abortable-promise.js' +import { makeAbortedRequest } from './fixtures/make-aborted-request.js' import type { BlockRetriever, Helia } from '@helia/interface' -async function makeAbortedRequest (verifiedFetch: VerifiedFetch, [resource, options = {}]: Parameters, promise: Promise): Promise { - const controller = new AbortController() - const resultPromise = verifiedFetch.fetch(resource, { - ...options, - signal: controller.signal - }) - - void promise.then(() => { - controller.abort() - }) - return resultPromise -} - -/** - * we need to emulate signal handling (blockBrokers/dnsResolvers/etc should handle abort signals too) - * this is a simplified version of what libs we depend on should do, and the - * tests in this file verify how verified-fetch would handle the failure - */ -async function getAbortablePromise (signal?: AbortSignal): Promise { - return new Promise((resolve, reject) => { - const timeoutId = setTimeout(() => { - reject(new Error('timeout while resolving')) - }, 5000) - - signal?.addEventListener('abort', () => { - clearTimeout(timeoutId) - reject(new Error('aborted')) - }) - }) -} - describe('abort-handling', function () { this.timeout(500) // these tests should all fail extremely quickly. if they don't, they're not aborting properly, or they're being ran on an extremely slow machine. const sandbox = Sinon.createSandbox() diff --git a/packages/verified-fetch/test/cache-control-header.spec.ts b/packages/verified-fetch/test/cache-control-header.spec.ts index 5c234fea..6bd84e54 100644 --- a/packages/verified-fetch/test/cache-control-header.spec.ts +++ b/packages/verified-fetch/test/cache-control-header.spec.ts @@ -5,23 +5,13 @@ import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { dns } from '@multiformats/dns' import { expect } from 'aegir/chai' import Sinon from 'sinon' -import { stubInterface } from 'sinon-ts' import { VerifiedFetch } from '../src/verified-fetch.js' import { createHelia } from './fixtures/create-offline-helia.js' +import { answerFake } from './fixtures/dns-answer-fake.js' import type { Helia } from '@helia/interface' import type { IPNS } from '@helia/ipns' import type { DNSResponse } from '@multiformats/dns' -function answerFake (data: string, TTL: number, name: string, type: number): DNSResponse { - const fake = stubInterface() - fake.Answer = [{ - data, - TTL, - name, - type - }] - return fake -} describe('cache-control header', () => { let helia: Helia let name: IPNS diff --git a/packages/verified-fetch/test/fixtures/get-abortable-promise.ts b/packages/verified-fetch/test/fixtures/get-abortable-promise.ts new file mode 100644 index 00000000..65ca6624 --- /dev/null +++ b/packages/verified-fetch/test/fixtures/get-abortable-promise.ts @@ -0,0 +1,17 @@ +/** + * we need to emulate signal handling (blockBrokers/dnsResolvers/etc should handle abort signals too) + * this is a simplified version of what libs we depend on should do, and the + * tests in this file verify how verified-fetch would handle the failure + */ +export async function getAbortablePromise (signal?: AbortSignal): Promise { + return new Promise((resolve, reject) => { + const timeoutId = setTimeout(() => { + reject(new Error('timeout while resolving')) + }, 5000) + + signal?.addEventListener('abort', () => { + clearTimeout(timeoutId) + reject(new Error('aborted')) + }) + }) +} diff --git a/packages/verified-fetch/test/fixtures/make-aborted-request.ts b/packages/verified-fetch/test/fixtures/make-aborted-request.ts new file mode 100644 index 00000000..6fc8ac73 --- /dev/null +++ b/packages/verified-fetch/test/fixtures/make-aborted-request.ts @@ -0,0 +1,14 @@ +import type { VerifiedFetch } from '../../src/verified-fetch.js' + +export async function makeAbortedRequest (verifiedFetch: VerifiedFetch, [resource, options = {}]: Parameters, promise: Promise): Promise { + const controller = new AbortController() + const resultPromise = verifiedFetch.fetch(resource, { + ...options, + signal: controller.signal + }) + + void promise.then(() => { + controller.abort() + }) + return resultPromise +}