0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-06 22:40:26 -05:00
verdaccio/packages/core/tarball/tests/convertDistRemoteToLocalTarballUrls.spec.ts
Marc Bernard 5cfbc1485d
fix(store): add logging when requesting tarball (#4847)
* fix(api): error handling when requesting tarball

* add tarball test for scoped packages

* update changeset

* add logging for tarball requests

* Revert

* Update violet-boxes-float.md
2024-09-28 12:47:45 +02:00

118 lines
3.6 KiB
TypeScript

import * as httpMocks from 'node-mocks-http';
import { describe, expect, test } from 'vitest';
import { HEADERS } from '@verdaccio/core';
import { convertDistRemoteToLocalTarballUrls } from '../src';
describe('convertDistRemoteToLocalTarballUrls', () => {
const fakeHost = 'fake.com';
const buildURI = (host, version) => `http://${host}/npm_test/-/npm_test-${version}.tgz`;
const cloneMetadata = (pkg = metadata) => Object.assign({}, pkg);
const metadata: any = {
name: 'npm_test',
versions: {
'1.0.0': {
dist: {
tarball: 'http://registry.org/npm_test/-/npm_test-1.0.0.tgz',
},
},
'1.0.1': {
dist: {
tarball: 'http://registry.org/npm_test/-/npm_test-1.0.1.tgz',
},
},
},
};
test('should build a URI for dist tarball based on new domain', () => {
const req = httpMocks.createRequest({
method: 'GET',
headers: {
host: fakeHost,
[HEADERS.FORWARDED_PROTO]: 'http',
},
url: '/',
});
const convertDist = convertDistRemoteToLocalTarballUrls(cloneMetadata(), {
host: req.hostname,
headers: req.headers as any,
protocol: req.protocol,
});
expect(convertDist.versions['1.0.0'].dist.tarball).toEqual(buildURI(fakeHost, '1.0.0'));
expect(convertDist.versions['1.0.1'].dist.tarball).toEqual(buildURI(fakeHost, '1.0.1'));
});
test('should return same URI whether host is missing', () => {
const req = httpMocks.createRequest({
method: 'GET',
headers: {
host: 'http',
},
url: '/',
});
const convertDist = convertDistRemoteToLocalTarballUrls(cloneMetadata(), {
host: req.hostname,
headers: req.headers as any,
protocol: req.protocol,
});
expect(convertDist.versions['1.0.0'].dist.tarball).toEqual(
convertDist.versions['1.0.0'].dist.tarball
);
});
test('should return same URI whether host is undefined', () => {
const req = httpMocks.createRequest({
method: 'GET',
headers: {
host: undefined,
},
url: '/',
});
const convertDist = convertDistRemoteToLocalTarballUrls(cloneMetadata(), {
host: req.hostname,
headers: req.headers as any,
protocol: req.protocol,
});
expect(convertDist.versions['1.0.0'].dist.tarball).toEqual(
convertDist.versions['1.0.0'].dist.tarball
);
});
});
describe('convertDistRemoteToLocalTarballUrls - scoped', () => {
const fakeHost = 'fake.com';
const buildURI = (host, version) => `http://${host}/@org/npm_test/-/npm_test-${version}.tgz`;
const cloneMetadata = (pkg = metadata) => Object.assign({}, pkg);
const metadata: any = {
name: '@org/npm_test',
versions: {
'1.0.0': {
dist: {
tarball: 'http://registry.org/@org/npm_test/-/npm_test-1.0.0.tgz',
},
},
'1.0.1': {
dist: {
tarball: 'http://registry.org/@org/npm_test/-/npm_test-1.0.1.tgz',
},
},
},
};
test('should build a URI for dist tarball based on new domain', () => {
const req = httpMocks.createRequest({
method: 'GET',
headers: {
host: fakeHost,
[HEADERS.FORWARDED_PROTO]: 'http',
},
url: '/',
});
const convertDist = convertDistRemoteToLocalTarballUrls(cloneMetadata(), {
host: req.hostname,
headers: req.headers as any,
protocol: req.protocol,
});
expect(convertDist.versions['1.0.0'].dist.tarball).toEqual(buildURI(fakeHost, '1.0.0'));
expect(convertDist.versions['1.0.1'].dist.tarball).toEqual(buildURI(fakeHost, '1.0.1'));
});
});