From 74c381c6a4e2aeab2b11f024775d376090c17921 Mon Sep 17 00:00:00 2001 From: Adrien Castex Date: Mon, 26 Jun 2017 12:23:18 +0200 Subject: [PATCH] Upgraded the test framework to be more clean and ease the making of new tests --- test/v2/root.js | 236 +++++++++++++++++++++++++++++++++++++++ test/v2/tests.ts/Type.ts | 30 +++++ 2 files changed, 266 insertions(+) create mode 100644 test/v2/root.js create mode 100644 test/v2/tests.ts/Type.ts diff --git a/test/v2/root.js b/test/v2/root.js new file mode 100644 index 00000000..6ad0910f --- /dev/null +++ b/test/v2/root.js @@ -0,0 +1,236 @@ +"use strict"; +var webdav = require('../../lib/index.js').v2, + request = require('request'), + path = require('path'), + fs = require('fs') + +module.exports = (callback, options) => { + var successes = []; + var errors = []; + + function success(text) + { + successes.push(' \x1b[42m\x1b[37m\x1b[1m o \x1b[0m ' + text + '\x1b[0m'); + } + function error(text) + { + errors.push(' \x1b[41m\x1b[37m\x1b[1m x \x1b[0m ' + text + '\x1b[0m'); + } + + var nb = 0; + + function callCallback() + { + --nb; + if(nb <= 0) + callback(successes, errors); + } + + var root = path.join(__dirname, 'tests'); + fs.readdir(root, (e, files) => { + if(e) + throw e; + + files = files.filter((f) => f.indexOf('.js') === -1); + nb += files.length; + let gindex = 0; + files.forEach((f1) => { + const f1x = path.join(root, f1); + fs.readdir(f1x, (e, files) => { + if(e) + throw e; + + files = files.filter((f) => f.indexOf('.js') === f.length - 3 && f.indexOf('.') !== 0); + nb += files.length; + files.forEach((f) => { + const fx = path.join(f1x, f); + + const info = { + name: undefined, + options, + port: options.port + gindex * 10, + easyError: (e, cb) => (e, arg1, arg2, arg3) => { + if(e) + info.isValid(false, e); + else + cb(arg1, arg2, arg3); + }, + startServer: (options, autoStart = true) => { + options = options ? options : {}; + options.port = info.port + info.servers.length; + const startServer = new webdav.WebDAVServer(options); + if(autoStart) + startServer.start(); + info.servers.push(startServer); + return startServer; + }, + reqStream: (config, callback) => { + const stream = request(config); + stream.on('error', (e) => { + info.isValid(false, 'HTTP error.', e); + }) + stream.on('complete', (res, body) => { + if(res.statusCode >= 300) + return info.isValid(false, res.statusCode + ' - ' + res.statusMessage); + + callback(); + }) + return stream; + }, + req: (config, _codeStatusExpected, _callback) => { + const codeStatusExpected = _callback ? _codeStatusExpected : -1; + const callback = _callback ? _callback : _codeStatusExpected; + + request(config, (e, res, body) => { + if(e) + return info.isValid(false, 'HTTP error.', e); + if(codeStatusExpected === -1 && res.statusCode >= 300) + return info.isValid(false, res.statusCode + ' - ' + res.statusMessage); + if(codeStatusExpected !== -1 && res.statusCode != codeStatusExpected) + return info.isValid(false, 'Expected ' + codeStatusExpected + ' but got : ' + res.statusCode + ' - ' + res.statusMessage); + + if(body) + body = body.toString(); + + callback(res, body); + }) + }, + reqXML: (config, _codeStatusExpected, _callback) => { + const codeStatusExpected = _callback ? _codeStatusExpected : -1; + const callback = _callback ? _callback : _codeStatusExpected; + + request(config, (e, res, body) => { + if(e) + return info.isValid(false, 'HTTP error.', e); + if(codeStatusExpected === -1 && res.statusCode >= 300) + return info.isValid(false, res.statusCode + ' - ' + res.statusMessage); + if(codeStatusExpected !== -1 && res.statusCode != codeStatusExpected) + return info.isValid(false, 'Expected ' + codeStatusExpected + ' but got : ' + res.statusCode + ' - ' + res.statusMessage); + + if(body) + { + try + { + body = webdav.XML.parse(body); + } + catch(ex) + { + return info.isValid(false, 'Invlid XML in the response body.', body.toString()); + } + } + + callback(res, body); + }) + }, + init: (nbExpected, name) => { + if(name !== undefined) + { + if(name.constructor === String) + { + info.name = name; + info.startServer(); + } + else + { + name.port = info.port; + info.startServer(name); + } + } + else + info.startServer(); + info.ctx = webdav.RequestContext.createFake(info.servers[0]); + info.expect(nbExpected); + return info.servers[0]; + }, + servers: [], + expect: (nb) => { + let callback = (valid, details) => { + callback = (valid, details) => { } + + details = details ? ' :: ' + details : ''; + const prefix = '[' + f1.replace(/([A-Z-])/g, ' $1').trim().toLowerCase() + ' :: ' + f.replace('.js', '').replace(/([A-Z-])/g, ' $1').trim().toLowerCase() + (info.name ? ' : ' + info.name : '') + ']'; + if(valid) + success(prefix + details) + else + error(prefix + details) + callCallback(); + } + + var allGood = true; + var allMsg; + info.isValid = function(good, msg, error) + { + if(error) + { + msg += ' :: ' + error; + if(options.showExceptions) + console.error(error); + } + + --nb; + if(msg && allGood && !good) + allMsg = msg; + allGood = allGood && good; + if(nb === 0) + { + info.servers.forEach((s) => s.stop()); + callback(allGood, allMsg); + } + } + } + }; + + try + { + setTimeout(() => info.isValid(false, 'Timeout'), options.timeout); + process.nextTick(() => require(fx).default(info, (good, msg, e) => info.isValid(good, msg, e))); + } + catch(ex) + { + if(options.showExceptions) + console.error(ex); + + error(info.name + '\r\n' + ex) + callCallback(); + } + ++gindex; + }) + + --nb; + }) + }) + }) +}; + +if(!module.parent) + module.exports((successes, errors) => { + console.log('====================================='); + console.log('==== webdav-server === Version 2 ===='); + console.log('====================================='); + console.log(); + console.log(' ' + successes.length + ' successe(s).'); + console.log(' ' + errors.length + ' error(s).'); + + if(successes.length) + { + console.log(); + console.log(' Successe(s) :'); + successes.sort().forEach(v => console.log(v)); + } + if(errors.length) + { + console.log(); + console.log(' Error(s) :'); + errors.sort().forEach(v => console.log(v)); + } + + console.log(); + console.log(' ' + successes.length + ' successe(s).'); + console.log(' ' + errors.length + ' error(s).'); + + process.exit(errors.length > 0 ? 1 : 0); + }, { + port: 1900, + showExceptions : true, + timeout: 6000 + }) diff --git a/test/v2/tests.ts/Type.ts b/test/v2/tests.ts/Type.ts new file mode 100644 index 00000000..4564f9ff --- /dev/null +++ b/test/v2/tests.ts/Type.ts @@ -0,0 +1,30 @@ +import { v2 } from '../../../lib/index.js' +import { RequestResponse, Options, Request } from 'request' + +export interface TestInfo +{ + port : number + ctx : v2.RequestContext + easyError : (e : Error, cb : () => void) => (e, arg1, arg2, arg3) => void + startServer : (options ?: v2.WebDAVServerOptions, autoStart ?: boolean) => v2.WebDAVServer + init : (nbExpected : number, name ?: v2.WebDAVServerOptions | string) => v2.WebDAVServer + req : { + (config : Options, callback : (res : RequestResponse, body ?: string) => void) : void + (config : Options, codeStatusExpected : number, callback : (res : RequestResponse, body ?: string) => void) : void + } + reqXML : { + (config : Options, callback : (res : RequestResponse, body ?: v2.XMLElement) => void) : void + (config : Options, codeStatusExpected : number, callback : (res : RequestResponse, body ?: v2.XMLElement) => void) : void + } + reqStream : (config : Options, callback : () => void) => Request +} + +export interface TestCallback +{ + (good : boolean, msg ?: string | Error, error ?: Error | string) : void +} + +export interface Test +{ + (info : TestInfo, isValid : TestCallback) : void +}