diff --git a/lib/util/http/queue.spec.ts b/lib/util/http/queue.spec.ts index bda615afdf81b0f8afe782d3a4225b7f44c9cf78..b3a20cb6bc483426b344c89290c25cc995cbd649 100644 --- a/lib/util/http/queue.spec.ts +++ b/lib/util/http/queue.spec.ts @@ -1,7 +1,37 @@ -import { getQueue } from './queue'; +import { mocked } from '../../../test/util'; +import * as _hostRules from './host-rules'; +import { clear, getQueue } from './queue'; + +jest.mock('./host-rules'); + +const hostRules = mocked(_hostRules); describe('util/http/queue', () => { + beforeEach(() => { + hostRules.getRequestLimit.mockReturnValue(143); + clear(); + }); + it('returns null for invalid URL', () => { - expect(getQueue(null)).toBeNull(); + expect(getQueue('$#@!')).toBeNull(); + }); + + it('returns queue for valid url', () => { + const q1a = getQueue('https://example.com'); + const q1b = getQueue('https://example.com'); + + const q2a = getQueue('https://example.com:8080'); + const q2b = getQueue('https://example.com:8080'); + + expect(q1a).not.toBeNull(); + expect(q1a).toBe(q1b); + + expect(q2a).not.toBeNull(); + expect(q2a).toBe(q2b); + + expect(q1a).not.toBe(q2a); + expect(q1a).not.toBe(q2b); + expect(q1b).not.toBe(q2a); + expect(q1b).not.toBe(q2b); }); }); diff --git a/lib/util/http/queue.ts b/lib/util/http/queue.ts index 83ead52e0e2d37a774e5bc68381de1b54b2d738e..e24a588a6a7fa32a5b88068d958c9f47811a5f8d 100644 --- a/lib/util/http/queue.ts +++ b/lib/util/http/queue.ts @@ -1,19 +1,11 @@ -import URL from 'url'; import PQueue from 'p-queue'; +import { parseUrl } from '../url'; import { getRequestLimit } from './host-rules'; const hostQueues = new Map<string | null, PQueue | null>(); -function getUrlHost(url: string): string | null { - try { - return URL.parse(url).host; - } catch (e) { - return null; - } -} - export function getQueue(url: string): PQueue | null { - const host = getUrlHost(url); + const host = parseUrl(url)?.host; if (!host) { return null; }