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: Support redirections #341

Merged
merged 15 commits into from
Dec 29, 2023
32 changes: 20 additions & 12 deletions sources/httpUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {UsageError} from 'clipanion';
import {RequestOptions} from 'https';
import {IncomingMessage} from 'http';
import {UsageError} from 'clipanion';
import {RequestOptions} from 'https';
import {IncomingMessage, ClientRequest} from 'http';

export async function fetchUrlStream(url: string, options: RequestOptions = {}) {
if (process.env.COREPACK_ENABLE_NETWORK === `0`)
@@ -13,17 +13,25 @@ export async function fetchUrlStream(url: string, options: RequestOptions = {})
const proxyAgent = new ProxyAgent();

return new Promise<IncomingMessage>((resolve, reject) => {
const request = https.get(url, {...options, agent: proxyAgent}, response => {
const statusCode = response.statusCode;
if (statusCode != null && statusCode >= 200 && statusCode < 300)
return resolve(response);
const createRequest = (url: string) => {
const request: ClientRequest = https.get(url, {...options, agent: proxyAgent}, response => {
const statusCode = response.statusCode;

return reject(new Error(`Server answered with HTTP ${statusCode} when performing the request to ${url}; for troubleshooting help, see https://github.com/nodejs/corepack#troubleshooting`));
});
if ([301, 302, 307, 308].includes(statusCode as number))
return createRequest(response.headers.location as string);
guibwl marked this conversation as resolved.
Show resolved Hide resolved

request.on(`error`, err => {
reject(new Error(`Error when performing the request to ${url}; for troubleshooting help, see https://github.com/nodejs/corepack#troubleshooting`));
});
if (statusCode != null && statusCode >= 200 && statusCode < 300)
return resolve(response);

return reject(new Error(`Server answered with HTTP ${statusCode} when performing the request to ${url}; for troubleshooting help, see https://github.com/nodejs/corepack#troubleshooting`));
});

request.on(`error`, err => {
reject(new Error(`Error when performing the request to ${url}; for troubleshooting help, see https://github.com/nodejs/corepack#troubleshooting`));
});
};

createRequest(url);
});
}

91 changes: 91 additions & 0 deletions tests/httpUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import {jest, describe, beforeEach, beforeAll, it, expect} from '@jest/globals';

import {fetchUrlStream} from '../sources/httpUtils';


describe(`http utils fetchUrlStream`, () => {
const getUrl = (statusCode: number | string, redirectCode?: number | string) =>
`https://registry.example.org/answered/${statusCode}${redirectCode ? `?redirectCode=${redirectCode}` : ``}`;

const httpsGetFn = jest.fn((url: string, _, callback: (response: any) => void) => {
const parsedURL = new URL(url);
const statusCode = parsedURL.pathname.slice(parsedURL.pathname.lastIndexOf(`/`) + 1);
const response = {url, statusCode: +statusCode};
const errorCallbacks: Array<(err: string) => void> = [];

if ([301, 302, 307, 308].includes(+statusCode)) {
const redirectCode = parsedURL.searchParams.get(`redirectCode`)!;
// mock response.headers.location
Reflect.set(response, `headers`, {location: getUrl(redirectCode)});
guibwl marked this conversation as resolved.
Show resolved Hide resolved
}

// handle request.on('error', err => ...)
if (statusCode === `error`)
process.nextTick(() => errorCallbacks.forEach(cb => cb(`Test internal error`)));
else
callback(response);

return {
on: (type: string, callback: (err: string) => void) => {
if (type === `error`) {
errorCallbacks.push(callback);
}
},
};
});

beforeAll(() => {
jest.doMock(`https`, () => ({
get: httpsGetFn,
Agent: class Agent {},
}));
});

beforeEach(() => {
httpsGetFn.mockClear();
});

it(`correct response answered statusCode should be >= 200 and < 300`, async () => {
await expect(fetchUrlStream(getUrl(200))).resolves.toMatchObject({
statusCode: 200,
});

await expect(fetchUrlStream(getUrl(299))).resolves.toMatchObject({
statusCode: 299,
});

expect(httpsGetFn).toHaveBeenCalledTimes(2);
});

it(`bad response`, async () => {
await expect(fetchUrlStream(getUrl(300))).rejects.toThrowError();
await expect(fetchUrlStream(getUrl(199))).rejects.toThrowError();
});

it(`redirection with correct response`, async () => {
await expect(fetchUrlStream(getUrl(301, 200))).resolves.toMatchObject({
statusCode: 200,
});

expect(httpsGetFn).toHaveBeenCalledTimes(2);

await expect(fetchUrlStream(getUrl(308, 299))).resolves.toMatchObject({
statusCode: 299,
});

expect(httpsGetFn).toHaveBeenCalledTimes(4);
});

it(`redirection with bad response`, async () => {
await expect(fetchUrlStream(getUrl(301, 300))).rejects.toThrowError();
await expect(fetchUrlStream(getUrl(308, 199))).rejects.toThrowError();
guibwl marked this conversation as resolved.
Show resolved Hide resolved
});

it(`rejects with error`, async () => {
await expect(fetchUrlStream(getUrl(`error`))).rejects.toThrowError();
});

it(`rejects when redirection with error`, async () => {
await expect(fetchUrlStream(getUrl(307, `error`))).rejects.toThrowError();
});
});