mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-04-01 02:42:23 -05:00
test(unit): add test for uplink offline, refactor proxy
This commit is contained in:
parent
0cc29e605d
commit
bee4d1cf4f
4 changed files with 104 additions and 43 deletions
|
@ -373,28 +373,29 @@ class Storage {
|
|||
return options.callback(err);
|
||||
}
|
||||
|
||||
this._syncUplinksMetadata(options.name, data, {req: options.req}, function(err, result, uplink_errors) {
|
||||
if (err) {
|
||||
return options.callback(err);
|
||||
}
|
||||
|
||||
const propertyToKeep = [...WHITELIST];
|
||||
if (options.keepUpLinkData === true) {
|
||||
propertyToKeep.push('_uplinks');
|
||||
}
|
||||
|
||||
for (let i in result) {
|
||||
if (propertyToKeep.indexOf(i) === -1) { // Remove sections like '_uplinks' from response
|
||||
delete result[i];
|
||||
this._syncUplinksMetadata(options.name, data, {req: options.req},
|
||||
function getPackageSynUpLinksCallback(err, result, uplink_errors) {
|
||||
if (err) {
|
||||
return options.callback(err);
|
||||
}
|
||||
}
|
||||
|
||||
Utils.normalize_dist_tags(result);
|
||||
const propertyToKeep = [...WHITELIST];
|
||||
if (options.keepUpLinkData === true) {
|
||||
propertyToKeep.push('_uplinks');
|
||||
}
|
||||
|
||||
// npm can throw if this field doesn't exist
|
||||
result._attachments = {};
|
||||
for (let i in result) {
|
||||
if (propertyToKeep.indexOf(i) === -1) { // Remove sections like '_uplinks' from response
|
||||
delete result[i];
|
||||
}
|
||||
}
|
||||
|
||||
options.callback(null, result, uplink_errors);
|
||||
Utils.normalize_dist_tags(result);
|
||||
|
||||
// npm can throw if this field doesn't exist
|
||||
result._attachments = {};
|
||||
|
||||
options.callback(null, result, uplink_errors);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@ -585,7 +586,7 @@ class Storage {
|
|||
}, (err, upLinksErrors) => {
|
||||
assert(!err && Array.isArray(upLinksErrors));
|
||||
if (!exists) {
|
||||
return callback( Error[404]('no such package available')
|
||||
return callback( Utils.ErrorCode.get404('no such package available')
|
||||
, null
|
||||
, upLinksErrors );
|
||||
}
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
const JSONStream = require('JSONStream');
|
||||
const createError = require('http-errors');
|
||||
const _ = require('lodash');
|
||||
const request = require('request');
|
||||
const Stream = require('stream');
|
||||
const URL = require('url');
|
||||
import zlib from 'zlib';
|
||||
import JSONStream from 'JSONStream';
|
||||
import createError from 'http-errors';
|
||||
import _ from 'lodash';
|
||||
import request from 'request';
|
||||
import Stream from 'stream';
|
||||
import URL from 'url';
|
||||
import {parseInterval, is_object, ErrorCode} from './utils';
|
||||
import {ReadTarball} from '@verdaccio/streams';
|
||||
|
||||
const Logger = require('./logger');
|
||||
const MyStreams = require('@verdaccio/streams');
|
||||
const Utils = require('./utils');
|
||||
const zlib = require('zlib');
|
||||
|
||||
const encode = function(thing) {
|
||||
return encodeURIComponent(thing).replace(/^%40/, '@');
|
||||
|
@ -61,10 +62,10 @@ class ProxyStorage {
|
|||
}
|
||||
|
||||
// a bunch of different configurable timers
|
||||
this.maxage = Utils.parseInterval(setConfig(this.config, 'maxage', '2m' ));
|
||||
this.timeout = Utils.parseInterval(setConfig(this.config, 'timeout', '30s'));
|
||||
this.maxage = parseInterval(setConfig(this.config, 'maxage', '2m' ));
|
||||
this.timeout = parseInterval(setConfig(this.config, 'timeout', '30s'));
|
||||
this.max_fails = Number(setConfig(this.config, 'max_fails', 2 ));
|
||||
this.fail_timeout = Utils.parseInterval(setConfig(this.config, 'fail_timeout', '5m' ));
|
||||
this.fail_timeout = parseInterval(setConfig(this.config, 'fail_timeout', '5m' ));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -81,7 +82,7 @@ class ProxyStorage {
|
|||
|
||||
process.nextTick(function() {
|
||||
if (_.isFunction(cb)) {
|
||||
cb(createError('uplink is offline'));
|
||||
cb(ErrorCode.get500('uplink is offline'));
|
||||
}
|
||||
streamRead.emit('error', createError('uplink is offline'));
|
||||
});
|
||||
|
@ -107,7 +108,7 @@ class ProxyStorage {
|
|||
uri: uri,
|
||||
}, 'making request: \'@{method} @{uri}\'');
|
||||
|
||||
if (Utils.is_object(options.json)) {
|
||||
if (is_object(options.json)) {
|
||||
json = JSON.stringify(options.json);
|
||||
headers['Content-Type'] = headers['Content-Type'] || 'application/json';
|
||||
}
|
||||
|
@ -139,7 +140,7 @@ class ProxyStorage {
|
|||
}
|
||||
}
|
||||
|
||||
if (!err && Utils.is_object(body)) {
|
||||
if (!err && is_object(body)) {
|
||||
if (_.isString(body.error)) {
|
||||
error = body.error;
|
||||
}
|
||||
|
@ -181,7 +182,7 @@ class ProxyStorage {
|
|||
|
||||
let statusCalled = false;
|
||||
req.on('response', function(res) {
|
||||
if (!req._verdaccio_aborted && _.isNil(statusCalled) === false) {
|
||||
if (!req._verdaccio_aborted && !statusCalled) {
|
||||
statusCalled = true;
|
||||
self._statusCheck(true);
|
||||
}
|
||||
|
@ -351,7 +352,7 @@ class ProxyStorage {
|
|||
return callback(err);
|
||||
}
|
||||
if (res.statusCode === 404) {
|
||||
return callback( createError[404]('package doesn\'t exist on uplink') );
|
||||
return callback( ErrorCode.get404('package doesn\'t exist on uplink'));
|
||||
}
|
||||
if (!(res.statusCode >= 200 && res.statusCode < 300)) {
|
||||
const error = createError(`bad status code: ${res.statusCode}`);
|
||||
|
@ -368,7 +369,7 @@ class ProxyStorage {
|
|||
* @return {Stream}
|
||||
*/
|
||||
fetchTarball(url) {
|
||||
const stream = new MyStreams.ReadTarball({});
|
||||
const stream = new ReadTarball({});
|
||||
let current_length = 0;
|
||||
let expected_length;
|
||||
|
||||
|
@ -383,7 +384,7 @@ class ProxyStorage {
|
|||
|
||||
readStream.on('response', function(res) {
|
||||
if (res.statusCode === 404) {
|
||||
return stream.emit('error', createError[404]('file doesn\'t exist on uplink'));
|
||||
return stream.emit('error', ErrorCode.get404('file doesn\'t exist on uplink'));
|
||||
}
|
||||
if (!(res.statusCode >= 200 && res.statusCode < 300)) {
|
||||
return stream.emit('error', createError('bad uplink status code: ' + res.statusCode));
|
||||
|
@ -429,7 +430,7 @@ class ProxyStorage {
|
|||
});
|
||||
|
||||
let parsePackage = (pkg) => {
|
||||
if (Utils.is_object(pkg)) {
|
||||
if (is_object(pkg)) {
|
||||
transformStream.emit('data', pkg);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -336,8 +336,8 @@ const ErrorCode = {
|
|||
get400: (customMessage) => {
|
||||
return createError(400, customMessage);
|
||||
},
|
||||
get500: () => {
|
||||
return createError(500);
|
||||
get500: (customMessage) => {
|
||||
return customMessage ? createError(500, customMessage) : createError(500);
|
||||
},
|
||||
get403: () => {
|
||||
return createError(403, 'can\'t use this filename');
|
||||
|
|
|
@ -15,19 +15,20 @@ describe('UpStorge', () => {
|
|||
};
|
||||
let generateProxy = (config = uplinkDefault) => {
|
||||
const appConfig: Config = new AppConfig(configExample);
|
||||
// config.self_path = path.join('../partials/store');
|
||||
|
||||
return new ProxyStorage(config, appConfig);
|
||||
}
|
||||
|
||||
test('should be defined', () => {
|
||||
const proxy = generateProxy();
|
||||
|
||||
expect(proxy).toBeDefined();
|
||||
});
|
||||
|
||||
describe('UpStorge::getRemoteMetadata', () => {
|
||||
test('should be get remote metadata', (done) => {
|
||||
const proxy = generateProxy();
|
||||
|
||||
proxy.getRemoteMetadata('jquery', {}, (err, data, etag) => {
|
||||
expect(err).toBeNull();
|
||||
expect(_.isString(etag)).toBeTruthy();
|
||||
|
@ -38,6 +39,7 @@ describe('UpStorge', () => {
|
|||
|
||||
test('should be get remote metadata with etag', (done) => {
|
||||
const proxy = generateProxy();
|
||||
|
||||
proxy.getRemoteMetadata('jquery', {etag: '123456'}, (err, data, etag) => {
|
||||
expect(err).toBeNull();
|
||||
expect(_.isString(etag)).toBeTruthy();
|
||||
|
@ -48,7 +50,8 @@ describe('UpStorge', () => {
|
|||
|
||||
test('should be get remote metadata package does not exist', (done) => {
|
||||
const proxy = generateProxy();
|
||||
proxy.getRemoteMetadata('@verdaccio/fake-package', {etag: '123456'}, (err, data, etag) => {
|
||||
|
||||
proxy.getRemoteMetadata('@verdaccio/fake-package', {etag: '123456'}, (err) => {
|
||||
expect(err).not.toBeNull();
|
||||
expect(err.statusCode).toBe(404);
|
||||
expect(err.message).toMatch(/package doesn't exist on uplink/);
|
||||
|
@ -75,6 +78,62 @@ describe('UpStorge', () => {
|
|||
});
|
||||
|
||||
});
|
||||
|
||||
test('should throw a 404 on fetch a tarball from uplink', (done) => {
|
||||
const proxy = generateProxy();
|
||||
const tarball:string = 'https://google.es/aaa/-/aaa-0.0.1.tgz';
|
||||
const stream = proxy.fetchTarball(tarball);
|
||||
|
||||
stream.on('error', function(err) {
|
||||
expect(err).not.toBeNull();
|
||||
expect(err.statusCode).toBe(404);
|
||||
expect(err.message).toMatch(/file doesn't exist on uplink/);
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
stream.on('content-length', function(contentLength) {
|
||||
expect(contentLength).toBeDefined();
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
test('should be offline uplink', (done) => {
|
||||
const proxy = generateProxy();
|
||||
const tarball:string = 'http://404.verdaccioo.com';
|
||||
const stream = proxy.fetchTarball(tarball);
|
||||
expect(proxy.failed_requests).toBe(0);
|
||||
|
||||
//to test a uplink is offline we have to be try 3 times
|
||||
//the default failed request are set to 2
|
||||
process.nextTick(function() {
|
||||
stream.on('error', function(err) {
|
||||
expect(err).not.toBeNull();
|
||||
// expect(err.statusCode).toBe(404);
|
||||
expect(proxy.failed_requests).toBe(1);
|
||||
|
||||
const streamSecondTry = proxy.fetchTarball(tarball);
|
||||
streamSecondTry.on('error', function(err) {
|
||||
expect(err).not.toBeNull();
|
||||
/*
|
||||
code: 'ENOTFOUND',
|
||||
errno: 'ENOTFOUND',
|
||||
*/
|
||||
// expect(err.statusCode).toBe(404);
|
||||
expect(proxy.failed_requests).toBe(2);
|
||||
const streamThirdTry = proxy.fetchTarball(tarball);
|
||||
streamThirdTry.on('error', function(err) {
|
||||
expect(err).not.toBeNull();
|
||||
expect(err.statusCode).toBe(500);
|
||||
expect(proxy.failed_requests).toBe(2);
|
||||
expect(err.message).toMatch(/uplink is offline/);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue