1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-13 03:35:52 +01:00
verdaccio/packages/proxy/test/proxy.metadata.ts
Behrang Yarahmadi 13310814da
#2606 add prettier plugin sort imports (#2607)
* #2606 add prettier plugin sort imprts

* #2606 update pnpm-lock.yaml

* #2606 update eslint rules

* #2606 fixes website directory formatting

Co-authored-by: Ayush Sharma <ayush.sharma@trivago.com>
2021-10-29 17:33:05 +02:00

134 lines
4.1 KiB
TypeScript

import nock from 'nock';
import path from 'path';
import { Config, parseConfigFile } from '@verdaccio/config';
import { API_ERROR, errorUtils } from '@verdaccio/core';
import { ProxyStorage } from '../src/up-storage';
const getConf = (name) => path.join(__dirname, '/conf', name);
const mockDebug = jest.fn();
const mockInfo = jest.fn();
const mockHttp = jest.fn();
const mockError = jest.fn();
const mockWarn = jest.fn();
jest.mock('@verdaccio/logger', () => {
const originalLogger = jest.requireActual('@verdaccio/logger');
return {
...originalLogger,
logger: {
child: () => ({
debug: (arg) => mockDebug(arg),
info: (arg) => mockInfo(arg),
http: (arg) => mockHttp(arg),
error: (arg) => mockError(arg),
warn: (arg) => mockWarn(arg),
}),
},
};
});
const domain = 'https://registry.npmjs.org';
describe('proxy', () => {
beforeEach(() => {
nock.cleanAll();
});
const defaultRequestOptions = {
url: 'https://registry.npmjs.org',
};
const proxyPath = getConf('proxy1.yaml');
const conf = new Config(parseConfigFile(proxyPath));
describe('getRemoteMetadata', () => {
describe('basic requests', () => {
test('proxy call with etag', (done) => {
nock(domain)
.get('/jquery')
.reply(
200,
{ body: 'test' },
{
etag: () => `_ref_4444`,
}
);
const prox1 = new ProxyStorage(defaultRequestOptions, conf);
prox1.getRemoteMetadata('jquery', {}, (_error, body, etag) => {
expect(etag).toEqual('_ref_4444');
expect(body).toEqual({ body: 'test' });
done();
});
});
test('proxy call with etag as option', (done) => {
nock(domain)
.get('/jquery')
.reply(
200,
{ body: 'test' },
{
etag: () => `_ref_4444`,
}
);
const prox1 = new ProxyStorage(defaultRequestOptions, conf);
prox1.getRemoteMetadata('jquery', { etag: 'rev_3333' }, (_error, body, etag) => {
expect(etag).toEqual('_ref_4444');
expect(body).toEqual({ body: 'test' });
done();
});
});
test('proxy not found', (done) => {
nock(domain).get('/jquery').reply(404);
const prox1 = new ProxyStorage(defaultRequestOptions, conf);
prox1.getRemoteMetadata('jquery', { etag: 'rev_3333' }, (error) => {
expect(error).toEqual(errorUtils.getNotFound(API_ERROR.NOT_PACKAGE_UPLINK));
done();
});
});
});
describe('error handling', () => {
test('reply with error', (done) => {
nock(domain).get('/jquery').replyWithError('something awful happened');
const prox1 = new ProxyStorage(defaultRequestOptions, conf);
prox1.getRemoteMetadata('jquery', {}, (error) => {
expect(error).toEqual(new Error('something awful happened'));
done();
});
});
test('reply with bad body json format', (done) => {
nock(domain).get('/jquery').reply(200, 'some-text');
const prox1 = new ProxyStorage(defaultRequestOptions, conf);
prox1.getRemoteMetadata('jquery', {}, (error) => {
expect(error).toEqual(new SyntaxError('Unexpected token s in JSON at position 0'));
done();
});
});
test('400 error proxy call', (done) => {
nock(domain).get('/jquery').reply(409);
const prox1 = new ProxyStorage(defaultRequestOptions, conf);
prox1.getRemoteMetadata('jquery', {}, (error) => {
expect(error.statusCode).toEqual(500);
expect(mockInfo).toHaveBeenCalled();
expect(mockHttp).toHaveBeenCalledWith({
request: { method: 'GET', url: 'https://registry.npmjs.org/jquery' },
status: 409,
});
expect(mockHttp).toHaveBeenCalledWith({
bytes: { in: 0, out: 0 },
err: undefined,
error: undefined,
request: { method: 'GET', url: 'https://registry.npmjs.org/jquery' },
status: 409,
});
done();
});
});
});
});
});