chore: add unit test for smart request

This commit is contained in:
Juan Picado @jotadeveloper 2017-11-27 07:15:09 +01:00 committed by juanpicado
parent adf7f3adb3
commit 86d92e96d2
8 changed files with 209 additions and 121 deletions

View File

@ -1,4 +1,4 @@
{
{
"env": {
"ui": {
"presets": [
@ -28,6 +28,7 @@
"test": {
"presets": [ "es2015-node4", "flow"],
"plugins": [
"transform-class-properties",
"transform-object-rest-spread"
]
},

View File

@ -57,6 +57,7 @@
"babel-loader": "7.1.2",
"babel-plugin-flow-runtime": "0.15.0",
"babel-plugin-transform-async-to-generator": "^6.24.1",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "1.3.4",
"babel-plugin-transform-es2015-classes": "^6.24.1",
"babel-plugin-transform-runtime": "6.23.0",
@ -79,7 +80,7 @@
"element-theme-default": "1.4.12",
"enzyme": "^3.1.0",
"enzyme-adapter-react-16": "^1.0.2",
"eslint": "4.12.1",
"eslint": "4.2.0",
"eslint-config-google": "0.9.1",
"eslint-loader": "1.9.0",
"eslint-plugin-babel": "4.1.2",

View File

@ -0,0 +1,103 @@
// @flow
import _ from 'lodash';
import rimRaf from 'rimraf';
import path from 'path';
import {fork} from 'child_process';
import Server from './server';
import type {IVerdaccioConfig, IServerBridge, IServerProcess} from './types';
export default class VerdaccioProcess implements IServerProcess {
bridge: IServerBridge;
config: IVerdaccioConfig;
childFork: any;
constructor(config: IVerdaccioConfig) {
console.log("----------<", config);
this.config = config;
this.bridge = new Server(config.domainPath);
}
init(): Promise<any> {
console.log("**VerdaccioProcess**:init");
return new Promise((resolve, reject) => {
const verdaccioRegisterWrap = path.join(__dirname, '/../../helper/verdaccio-test');
const storageDir: string = path.join(__dirname, `/../${this.config.storagePath}`);
const configPath: string = path.join(__dirname, '../', this.config.configPath);
rimRaf(storageDir, (err) => {
if (_.isNil(err) === false) {
reject(err);
}
// const filteredArguments = process.execArgv = process.execArgv.filter(function(x) {
// // filter out --debug-brk and --inspect-brk since Node7
// return (x.indexOf('--debug-brk') === -1 && x.indexOf('--inspect-brk') === -1);
// });
this.childFork = fork(verdaccioRegisterWrap,
['-c', configPath],
{
// silent: !process.env.TRAVIS,
silent: false,
env: {
BABEL_ENV: 'registry'
}
}
);
// forks.push(childFork);
this.childFork.on('message', function(msg) {
console.log("**VerdaccioProcess**:message", msg);
if ('verdaccio_started' in msg) {
resolve(this.childFork);
}
});
this.childFork.on('error', function(err) {
console.log("**VerdaccioProcess**:error",err);
reject(err);
});
this.childFork.on('disconnect', function(err) {
console.log("**VerdaccioProcess**:disconnect",err);
reject(err);
});
this.childFork.on('exit', function(err) {
console.log("**VerdaccioProcess**:exit",err);
reject(err);
});
//process.execArgv = filteredArguments;
});
});
}
getBridge(): IServerBridge {
return this.bridge;
}
stop(): Promise<any> {
return new Promise(function(resolve, reject) {
});
}
notify(callback: Function): void {
callback();
}
cleanStorage(): Promise<any> {
return new Promise(function(resolve, reject) {
rimRaf(this.config.storagePath, function(err) {
if(_.isNil(err) === false) {
reject(err);
}
});
});
}
}

View File

@ -0,0 +1,20 @@
// @flow
import express from 'express';
import bodyParser from 'body-parser';
export class ExpressServer {
static start(): Promise<any> {
return new Promise(function(resolve, reject) {
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.listen(55550, function starExpressServer() {
resolve();
});
});
}
}

View File

@ -1,29 +1,7 @@
// @flow
import _ from 'lodash';
import express from 'express';
import rimRaf from 'rimraf';
import path from 'path';
import {fork} from 'child_process';
import bodyParser from 'body-parser';
import Server from './server';
import type {IVerdaccioConfig, IServerBridge, IServerProcess} from './types';
export class ExpressServer {
static start(): Promise<any> {
return new Promise(function(resolve, reject) {
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.listen(55550, function starExpressServer() {
resolve();
});
});
}
}
import VerdaccioProcess from './server_process';
import type {IVerdaccioConfig, IServerProcess} from './types';
export class VerdaccioConfig implements IVerdaccioConfig {
@ -38,116 +16,34 @@ export class VerdaccioConfig implements IVerdaccioConfig {
}
}
class VerdaccioProcess implements IServerProcess {
bridge: IServerBridge;
config: IVerdaccioConfig;
childFork: any;
constructor(config: IVerdaccioConfig) {
this.config = config;
this.bridge = new Server(config.domainPath);
}
init(): Promise<any> {
return new Promise(function(resolve, reject) {
const verdaccioRegisterWrap = path.join(__dirname, '/../../helper/verdaccio-test');
const storageDir: string = path.join(__dirname, `/../${this.config.storagePath}`);
const configPath: string = path.join(__dirname, '../', this.config.configPath);
rimRaf(storageDir, function(err) {
if (_.isNil(err) === false) {
reject(err);
}
// const filteredArguments = process.execArgv = process.execArgv.filter(function(x) {
// // filter out --debug-brk and --inspect-brk since Node7
// return (x.indexOf('--debug-brk') === -1 && x.indexOf('--inspect-brk') === -1);
// });
this.childFork = fork(verdaccioRegisterWrap,
['-c', configPath],
{
// silent: !process.env.TRAVIS,
silent: false,
env: {
BABEL_ENV: 'registry'
}
}
);
// forks.push(childFork);
this.childFork.on('message', function(msg) {
if ('verdaccio_started' in msg) {
resolve(this.childFork);
}
});
this.childFork.on('error', function(err) {
reject(err);
});
this.childFork.on('disconnect', function(err) {
reject(err);
});
this.childFork.on('exit', function(err) {
reject(err);
});
//process.execArgv = filteredArguments;
});
});
}
getBridge(): IServerBridge {
return this.bridge;
}
stop(): Promise<any> {
return new Promise(function(resolve, reject) {
});
}
notify(callback: Function): void {
callback();
}
cleanStorage(): Promise<any> {
return new Promise(function(resolve, reject) {
rimRaf(this.config.storagePath, function(err) {
if(_.isNil(err) === false) {
reject(err);
}
});
});
}
}
export class VerdaccioServer {
serverProcess: IServerProcess;
pid: number;
constructor(config: IVerdaccioConfig) {
console.log("*************VerdaccioServer****************");
this.serverProcess = new VerdaccioProcess(config);
console.log("*************VerdaccioServer****************");
}
start(): Promise<any> {
console.log("*************VerdaccioServer******start**********");
return this.serverProcess.init().then(this.debugCheck);
}
debugCheck(): Promise<any>{
debugCheck = (): Promise<any> => {
console.log("*************VerdaccioServer******debugCheck**********");
return new Promise((resolve, reject) => {
this.serverProcess.getBridge().debug().status(200).then((body) => {
if (_.isNil(body.pid)) {
reject();
}
console.log("*************body.pid;******debugCheck**********", body.pid);
this.pid = body.pid;
console.log("************authTestUser***");
return this.authTestUser().catch(function(reason: any) {
console.log("************authTestUser**reason*", reason);
reject(reason);
});
}).catch(function(reason: any) {

View File

@ -1,5 +1,3 @@
'use strict';
const assert = require('assert');
const Utils = require('../../src/lib/utils');
const Config = require('../../src/lib/config');

View File

@ -0,0 +1,69 @@
import _ from 'lodash';
import smartRequest, {PromiseAssert} from '../../functional/lib/request';
import type {IRequestPromise} from './../functional/types';
describe('Request Functional', () => {
const restTest: string = "http://registry.npmjs.org/aaa";
describe('Request Functional', () => {
test('PromiseAssert', () => {
expect(_.isFunction(smartRequest)).toBeTruthy();
});
test('basic resolve', (done) => {
const requestPromise: IRequestPromise = new PromiseAssert((resolve, reject) => {
resolve(1);
});
requestPromise.then((result) => {
expect(result).toBe(1);
done();
});
});
});
describe('smartRequest Rest', () => {
test('basic rest', (done) => {
const options: any = {
url: restTest,
method: 'GET'
};
smartRequest(options).then((result)=> {
expect(_.isString(result)).toBeTruthy();
done();
})
});
describe('smartRequest Status', () => {
test('basic check status 200', (done) => {
const options: any = {
url: restTest,
method: 'GET'
};
smartRequest(options).status(200).then((result)=> {
expect(JSON.parse(result).name).toBe('aaa');
done();
})
});
test('basic check status 404', (done) => {
const options: any = {
url: 'http://www.google.fake',
method: 'GET'
};
smartRequest(options).status(404).then((result)=> {
// this never is resolved
}, function(error) {
expect(error.code).toBe('ENOTFOUND');
done();
})
});
});
});
});

View File

@ -28,9 +28,9 @@
version "0.0.2"
resolved "https://registry.npmjs.org/@verdaccio/streams/-/streams-0.0.2.tgz#72cd65449e657b462a1ca094f663cad9ea872427"
"@verdaccio/types@^0.0.7":
version "0.0.7"
resolved "https://registry.npmjs.org/@verdaccio/types/-/types-0.0.7.tgz#d9db7775eb87a288fbe4e58255431d3e1e9520f7"
"@verdaccio/types@^0.0.8":
version "0.0.8"
resolved "https://registry.npmjs.org/@verdaccio/types/-/types-0.0.8.tgz#d457015e7eef2c162a4a44eef3d0233fc6c40fc2"
JSONStream@^0.8.4:
version "0.8.4"