diff --git a/src/utils/Fetcher.test.ts b/src/utils/Fetcher.test.ts index 6b106a7..60a049e 100644 --- a/src/utils/Fetcher.test.ts +++ b/src/utils/Fetcher.test.ts @@ -21,8 +21,8 @@ describe('fetch', () => { const mockPool = mockAgent.get('https://some-url.test'); mockPool.intercept({ path: '/' }).reply(200, 'some text'); - const responseText1 = await fetcher.text(ctx, new URL('https://some-url.test/')); - const responseText2 = await fetcher.text(ctx, new URL('https://some-url.test/'), { headers: { 'User-Agent': 'jest' } }); + const responseText1 = await fetcher.text(ctx, new URL('https://user:pass@some-url.test/')); + const responseText2 = await fetcher.text(ctx, new URL('https://user:pass@some-url.test/'), { headers: { 'User-Agent': 'jest' } }); expect(responseText1).toBe('some text'); expect(responseText2).toStrictEqual(responseText1); @@ -30,6 +30,7 @@ describe('fetch', () => { expect(mockAgent.getCallHistory()?.firstCall()?.headers).toMatchObject({ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'en', + 'Authorization': 'Basic dXNlcjpwYXNz', 'Forwarded': 'for=0.0.0.0', 'Priority': 'u=0', 'User-Agent': 'node', diff --git a/src/utils/Fetcher.ts b/src/utils/Fetcher.ts index 034a709..c1f76cc 100644 --- a/src/utils/Fetcher.ts +++ b/src/utils/Fetcher.ts @@ -96,6 +96,7 @@ export class Fetcher { headers: { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'en', + ...(url.username && { Authorization: 'Basic ' + Buffer.from(`${url.username}:${url.password}`).toString('base64') }), 'Priority': 'u=0', 'User-Agent': this.hostUserAgentMap.get(url.host) ?? 'node', ...(cookieString && { Cookie: cookieString }), @@ -257,7 +258,13 @@ export class Fetcher { let response; try { - response = await fetch(url, { ...init, keepalive: true, signal: AbortSignal.timeout(init?.timeout ?? this.DEFAULT_TIMEOUT) }); + const finalUrl = new URL(url.href); + finalUrl.username = ''; + finalUrl.password = ''; + + const finalInit = { ...init, keepalive: true, signal: AbortSignal.timeout(init?.timeout ?? this.DEFAULT_TIMEOUT) }; + + response = await fetch(finalUrl, finalInit); } catch (error) { if (error instanceof DOMException && error.name === 'TimeoutError') { await this.increaseTimeoutsCount(url);