From dd22aaddde37e5cdedaa50556a336e75037898f6 Mon Sep 17 00:00:00 2001 From: aacerox Date: Fri, 30 Jun 2023 13:54:14 +0200 Subject: [PATCH] refactor: align test to module build --- .mocharc.cjs | 2 +- test/server/mock-server.js | 195 ------------------ test/server/mock-server.mjs | 195 ++++++++++++++++++ ...ErrorHandlers.js => TestErrorHandlers.mjs} | 4 +- ...owsRedirect.js => TestFollowsRedirect.mjs} | 4 +- .../{TestGETMethod.js => TestGETMethod.mjs} | 4 +- ...dPromised.js => TestGETMethodPromised.mjs} | 4 +- .../{TestPOSTMethod.js => TestPOSTMethod.mjs} | 4 +- ...Promised.js => TestPOSTMethodPromised.mjs} | 4 +- .../{TestRIOFacade.js => TestRIOFacade.mjs} | 4 +- test/test-proxy.js | 91 -------- test/test-proxy.mjs | 143 +++++++++++++ 12 files changed, 353 insertions(+), 301 deletions(-) delete mode 100644 test/server/mock-server.js create mode 100644 test/server/mock-server.mjs rename test/specs/{TestErrorHandlers.js => TestErrorHandlers.mjs} (91%) rename test/specs/{TestFollowsRedirect.js => TestFollowsRedirect.mjs} (94%) rename test/specs/{TestGETMethod.js => TestGETMethod.mjs} (98%) rename test/specs/{TestGETMethodPromised.js => TestGETMethodPromised.mjs} (98%) rename test/specs/{TestPOSTMethod.js => TestPOSTMethod.mjs} (98%) rename test/specs/{TestPOSTMethodPromised.js => TestPOSTMethodPromised.mjs} (98%) rename test/specs/{TestRIOFacade.js => TestRIOFacade.mjs} (98%) delete mode 100644 test/test-proxy.js create mode 100644 test/test-proxy.mjs diff --git a/.mocharc.cjs b/.mocharc.cjs index f53a328..6dc6c6c 100644 --- a/.mocharc.cjs +++ b/.mocharc.cjs @@ -3,7 +3,7 @@ module.exports = { require: 'should', reporter: 'spec', - spec: ['test/specs/*.js'], // the positional arguments! + spec: ['test/specs/*.mjs'], // the positional arguments! ui: 'bdd', recursive: true }; \ No newline at end of file diff --git a/test/server/mock-server.js b/test/server/mock-server.js deleted file mode 100644 index aaddb51..0000000 --- a/test/server/mock-server.js +++ /dev/null @@ -1,195 +0,0 @@ -import http from 'http' -import fs from 'fs' - -const RouterOptions = { - "baseMessageDir" : "", - "JSONMessageFile" : './test/server/message.json', - "XMLMessageFile" : './test/server/message.xml' - -} - -const RouteManager = { - "findRoute" : function(req, res) { - let handler = null - for ( let route in this.routes) { - if (req.url.startsWith(route)) { - handler = this.routes[route] - } - - } - if (!handler) - throw new Error("cannot find route " + req.url) - handler.call(this, req, res) - }, - "routes" : { - "/json" : function(req, res) { - const message = fs - .readFileSync(RouterOptions.JSONMessageFile, 'utf8') - res.writeHead(200, { - 'Content-Type' : 'application/json', - 'test-header' : 'test' - }) - res.write(message.toString()) - res.end() - }, - "/json/path" : function(req, res) { - const message = { - "url" : req.url - } - res.writeHead(200, { - 'Content-Type' : 'application/json', - 'test-header' : req.url - }) - res.write(JSON.stringify(message)) - res.end() - }, - "/xml" : function(req, res) { - const message = fs.readFileSync(RouterOptions.XMLMessageFile, 'utf8') - res.writeHead(200, { - 'Content-Type' : 'application/xml' - }) - res.write(message.toString()) - res.end() - }, - "/120/json?arg1=hello&arg2=world" : function(req, res) { - if (!req.headers["test-header"]) - throw new Error("no test-header found!!") - res.setHeader("test-response-header", req.headers["test-header"]) - this.routes["/json"](req, res) - }, - "/json?post" : function(req, res) { - req.on('data', function(data) { - res.writeHead(200, { - 'Content-Type' : 'application/json' - }) - res.write(data.toString()) - res.end() - }) - }, - "/json/path/post" : function(req, res) { - req.on('data', function(data) { - const message = { - "url" : req.url - } - - res.writeHead(200, { - 'Content-Type' : 'application/json' - }) - - message.postData = data.toString() - res.write(JSON.stringify(message)) - res.end() - }) - }, - "/json/error" : function(req, res) { - res.writeHead(500, {'Content-Type': 'text/plain'}) - res.end() - }, - "/xml/path/post" : function(req, res) { - req.on('data', function(data) { - res.writeHead(200, { - 'Content-Type' : 'application/xml' - }) - res.write(data.toString()) - res.end() - }) - }, - "/json/empty" : function(req, res) { - res.writeHead(200, { - 'Content-Type' : 'application/json' - }) - res.end() - }, - "/xml/empty" : function(req, res) { - res.writeHead(204, { - 'Content-Type' : 'application/xml' - }) - res.end() }, - "/json/contenttypewithspace" : function(req, res) { - const message = fs.readFileSync('./message.json', 'utf8') - res.writeHead(200, { - 'Content-Type' : 'application/json charset=utf-8' - }) - res.write(message.toString()) - res.end() - }, - "/json/test/content/type" : function(req, res) { - const message = fs.readFileSync(RouterOptions.JSONMessageFile, 'utf8') - res.writeHead(200, { - 'Content-Type' : 'test/json' - }) - res.write(message.toString()) - res.end() - }, - "/xml/test/content/type" : function(req, res) { - const message = fs.readFileSync(RouterOptions.XMLMessageFile, 'utf8') - res.writeHead(200, { - 'Content-Type' : 'test/xml' - }) - res.write(message.toString()) - res.end() - }, - "/followRedirects":function(req, res){ - - const repeatOffset = req.url.indexOf("?") - let repeat = parseInt(req.url.substring(repeatOffset + 1),10) - - - if (repeatOffset === 0){ - res.writeHead(301, { - 'Location':'http://localhost:4444/redirected' - }) - }else{ - if (repeat > 0){ - res.writeHead(301, { - 'Location':'http://localhost:4444/followRedirects?' + --repeat - }) - }else{ - res.writeHead(301, { - 'Location':'http://localhost:4444/redirected' - }) - } - - } - res.end() - }, - "/redirected":function(req, res){ - const message={"redirected":++this.redirectCount} - res.writeHead(200, { - 'Content-Type' : 'application/json charset=utf-8' - }) - res.write(JSON.stringify(message)) - res.end() - } - - }, - "sleep" : function(ms) { - const stop = new Date().getTime() - while (new Date().getTime() < stop + ms) { - // noop - } - }, - "redirectCount":0, - "redirectLimit":10 - -} - -// Create an HTTP server -let server = http.createServer(function(req, res) { - RouteManager.findRoute(req, res) -}) - -let mockServer = { - baseURL: "http://localhost:4444", - listen: function () { - server.listen.apply(server, arguments) - }, - close: function (callback) { - server.close(callback) - }, - on: function (event, cb) { - server.on.apply(server, event, cb) - } -} - -export default mockServer; \ No newline at end of file diff --git a/test/server/mock-server.mjs b/test/server/mock-server.mjs new file mode 100644 index 0000000..7dcfeac --- /dev/null +++ b/test/server/mock-server.mjs @@ -0,0 +1,195 @@ +import http from 'http'; +import fs from 'fs'; + +const RouterOptions = { + 'baseMessageDir': '', + 'JSONMessageFile': './test/server/message.json', + 'XMLMessageFile': './test/server/message.xml', + +}; + +const RouteManager = { + 'findRoute': function(req, res) { + let handler = null; + for ( const route in this.routes) { + if (req.url.startsWith(route)) { + handler = this.routes[route]; + } + } + if (!handler) { + throw new Error('cannot find route ' + req.url); + }; + handler.call(this, req, res); + }, + 'routes': { + '/json': function(req, res) { + const message = fs + .readFileSync(RouterOptions.JSONMessageFile, 'utf8'); + res.writeHead(200, { + 'Content-Type': 'application/json', + 'test-header': 'test', + }); + res.write(message.toString()); + res.end(); + }, + '/json/path': function(req, res) { + const message = { + 'url': req.url, + }; + res.writeHead(200, { + 'Content-Type': 'application/json', + 'test-header': req.url, + }); + res.write(JSON.stringify(message)); + res.end(); + }, + '/xml': function(req, res) { + const message = fs.readFileSync(RouterOptions.XMLMessageFile, 'utf8'); + res.writeHead(200, { + 'Content-Type': 'application/xml', + }); + res.write(message.toString()); + res.end(); + }, + '/120/json?arg1=hello&arg2=world': function(req, res) { + if (!req.headers['test-header']) { + throw new Error('no test-header found!!'); + }; + res.setHeader('test-response-header', req.headers['test-header']); + this.routes['/json'](req, res); + }, + '/json?post': function(req, res) { + req.on('data', function(data) { + res.writeHead(200, { + 'Content-Type': 'application/json', + }); + res.write(data.toString()); + res.end(); + }); + }, + '/json/path/post': function(req, res) { + req.on('data', function(data) { + const message = { + 'url': req.url, + }; + + res.writeHead(200, { + 'Content-Type': 'application/json', + }); + + message.postData = data.toString(); + res.write(JSON.stringify(message)); + res.end(); + }); + }, + '/json/error': function(req, res) { + res.writeHead(500, {'Content-Type': 'text/plain'}); + res.end(); + }, + '/xml/path/post': function(req, res) { + req.on('data', function(data) { + res.writeHead(200, { + 'Content-Type': 'application/xml', + }); + res.write(data.toString()); + res.end(); + }); + }, + '/json/empty': function(req, res) { + res.writeHead(200, { + 'Content-Type': 'application/json', + }); + res.end(); + }, + '/xml/empty': function(req, res) { + res.writeHead(204, { + 'Content-Type': 'application/xml', + }); + res.end(); + }, + '/json/contenttypewithspace': function(req, res) { + const message = fs.readFileSync('./message.json', 'utf8'); + res.writeHead(200, { + 'Content-Type': 'application/json charset=utf-8', + }); + res.write(message.toString()); + res.end(); + }, + '/json/test/content/type': function(req, res) { + const message = fs.readFileSync(RouterOptions.JSONMessageFile, 'utf8'); + res.writeHead(200, { + 'Content-Type': 'test/json', + }); + res.write(message.toString()); + res.end(); + }, + '/xml/test/content/type': function(req, res) { + const message = fs.readFileSync(RouterOptions.XMLMessageFile, 'utf8'); + res.writeHead(200, { + 'Content-Type': 'test/xml', + }); + res.write(message.toString()); + res.end(); + }, + '/followRedirects': function(req, res) { + const repeatOffset = req.url.indexOf('?'); + let repeat = parseInt(req.url.substring(repeatOffset + 1), 10); + + + if (repeatOffset === 0) { + res.writeHead(301, { + 'Location': 'http://localhost:4444/redirected', + }); + } else { + if (repeat > 0) { + res.writeHead(301, { + 'Location': 'http://localhost:4444/followRedirects?' + --repeat, + }); + } else { + res.writeHead(301, { + 'Location': 'http://localhost:4444/redirected', + }); + } + } + res.end(); + }, + '/redirected': function(req, res) { + const message={'redirected': ++this.redirectCount}; + res.writeHead(200, { + 'Content-Type': 'application/json charset=utf-8', + }); + res.write(JSON.stringify(message)); + res.end(); + }, + + }, + 'sleep': function(ms) { + const stop = new Date().getTime(); + while (new Date().getTime() < stop + ms) { + // noop + } + }, + 'redirectCount': 0, + 'redirectLimit': 10, + +}; + +// Create an HTTP server +const server = http.createServer(function(req, res) { + RouteManager.findRoute(req, res); +}); + +const mockServer = { + baseURL: 'http://localhost:4444', + listen: function(...args) { + server.listen(...args); + }, + close: function(callback) { + server.close(callback); + }, + on: function(event, cb) { + server.on.apply(server, event, cb); + }, +}; + +export default mockServer; diff --git a/test/specs/TestErrorHandlers.js b/test/specs/TestErrorHandlers.mjs similarity index 91% rename from test/specs/TestErrorHandlers.js rename to test/specs/TestErrorHandlers.mjs index 546a484..5ecd9f8 100644 --- a/test/specs/TestErrorHandlers.js +++ b/test/specs/TestErrorHandlers.mjs @@ -1,5 +1,5 @@ -import server from '../server/mock-server.js'; -import Client from '../../lib/NodeRestClient.js'; +import server from '../server/mock-server.mjs'; +import Client from '../../dist/mjs/NodeRestClient.js'; describe('Error Handlers', function() { diff --git a/test/specs/TestFollowsRedirect.js b/test/specs/TestFollowsRedirect.mjs similarity index 94% rename from test/specs/TestFollowsRedirect.js rename to test/specs/TestFollowsRedirect.mjs index d602cf3..82f5d29 100644 --- a/test/specs/TestFollowsRedirect.js +++ b/test/specs/TestFollowsRedirect.mjs @@ -1,5 +1,5 @@ -import server from '../server/mock-server.js'; -import Client from '../../lib/NodeRestClient.js'; +import server from '../server/mock-server.mjs'; +import Client from '../../dist/mjs/NodeRestClient.js'; describe('Follows Redirects', function() { // eslint-disable-next-line no-invalid-this diff --git a/test/specs/TestGETMethod.js b/test/specs/TestGETMethod.mjs similarity index 98% rename from test/specs/TestGETMethod.js rename to test/specs/TestGETMethod.mjs index cf4fe0e..d07b2dc 100644 --- a/test/specs/TestGETMethod.js +++ b/test/specs/TestGETMethod.mjs @@ -1,6 +1,6 @@ /* eslint-disable max-len */ -import server from '../server/mock-server.js'; -import Client from '../../lib/NodeRestClient.js'; +import server from '../server/mock-server.mjs'; +import Client from '../../dist/mjs/NodeRestClient.js'; describe('GET Method', function() { // eslint-disable-next-line no-invalid-this diff --git a/test/specs/TestGETMethodPromised.js b/test/specs/TestGETMethodPromised.mjs similarity index 98% rename from test/specs/TestGETMethodPromised.js rename to test/specs/TestGETMethodPromised.mjs index 857c623..be77eac 100644 --- a/test/specs/TestGETMethodPromised.js +++ b/test/specs/TestGETMethodPromised.mjs @@ -1,6 +1,6 @@ /* eslint-disable max-len */ -import server from '../server/mock-server.js'; -import Client from '../../lib/NodeRestClient.js'; +import server from '../server/mock-server.mjs'; +import Client from '../../dist/mjs/NodeRestClient.js'; let client; diff --git a/test/specs/TestPOSTMethod.js b/test/specs/TestPOSTMethod.mjs similarity index 98% rename from test/specs/TestPOSTMethod.js rename to test/specs/TestPOSTMethod.mjs index 7d4d1b4..99c576b 100644 --- a/test/specs/TestPOSTMethod.js +++ b/test/specs/TestPOSTMethod.mjs @@ -1,6 +1,6 @@ /* eslint-disable max-len */ -import server from '../server/mock-server.js'; -import Client from '../../lib/NodeRestClient.js'; +import server from '../server/mock-server.mjs'; +import Client from '../../dist/mjs/NodeRestClient.js'; describe('POST Method', function() { // eslint-disable-next-line no-invalid-this diff --git a/test/specs/TestPOSTMethodPromised.js b/test/specs/TestPOSTMethodPromised.mjs similarity index 98% rename from test/specs/TestPOSTMethodPromised.js rename to test/specs/TestPOSTMethodPromised.mjs index da82543..62e046b 100644 --- a/test/specs/TestPOSTMethodPromised.js +++ b/test/specs/TestPOSTMethodPromised.mjs @@ -1,6 +1,6 @@ /* eslint-disable max-len */ -import server from '../server/mock-server.js'; -import Client from '../../lib/NodeRestClient.js'; +import server from '../server/mock-server.mjs'; +import Client from '../../dist/mjs/NodeRestClient.js'; let client; diff --git a/test/specs/TestRIOFacade.js b/test/specs/TestRIOFacade.mjs similarity index 98% rename from test/specs/TestRIOFacade.js rename to test/specs/TestRIOFacade.mjs index 2268c29..b93119c 100644 --- a/test/specs/TestRIOFacade.js +++ b/test/specs/TestRIOFacade.mjs @@ -1,6 +1,6 @@ /* eslint-disable max-len */ -import server from '../server/mock-server.js'; -import Client from '../../lib/NodeRestClient.js'; +import server from '../server/mock-server.mjs'; +import Client from '../../dist/mjs/NodeRestClient.js'; describe('IO Facade', function() { // eslint-disable-next-line no-invalid-this diff --git a/test/test-proxy.js b/test/test-proxy.js deleted file mode 100644 index 1e374a7..0000000 --- a/test/test-proxy.js +++ /dev/null @@ -1,91 +0,0 @@ -import http from 'http'; -import fs from 'fs'; - -var blacklist = []; -var iplist = []; - -fs.watchFile('./blacklist', function(c,p) { update_blacklist(); }); -fs.watchFile('./iplist', function(c,p) { update_iplist(); }); - -function update_blacklist() { - console.log("Updating blacklist."); - blacklist = fs.readFileSync('./blacklist', {encoding: 'utf-8'}).split('\n') - .filter(function(rx) { return rx.length }) - .map(function(rx) { return RegExp(rx) }); -} - -function update_iplist() { - console.log("Updating iplist."); - iplist = fs.readFileSync('./iplist', {encoding: 'utf-8'}).split('\n') - .filter(function(rx) { return rx.length }); -} - -function ip_allowed(ip) { - for (i in iplist) { - if (iplist[i] == ip) { - return true; - } - } - return false; -} - -function host_allowed(host) { - for (i in blacklist) { - if (blacklist[i].test(host)) { - return false; - } - } - return true; -} - -function deny(response, msg) { - response.writeHead(401); - response.write(msg); - response.end(); -} - -http.createServer(function(request, response) { - var ip = request.socket.remoteAddress; - if (!ip_allowed(ip)) { - msg = "IP " + ip + " is not allowed to use this proxy"; - deny(response, msg); - console.log(msg); - return; - } - - if (!host_allowed(request.url)) { - msg = "Host " + request.url + " has been denied by proxy configuration"; - deny(response, msg); - console.log(msg); - return; - } - - console.log(ip + ": " + request.method + " " + request.url); - var agent = new http.Agent({ host: request.headers['host'], port: 80, maxSockets: 1 }); - var proxy_request = http.request({ - host: request.headers['host'], - port: 80, - method: request.method, - path: request.url, - headers: request.headers, - agent: agent - }); - proxy_request.addListener('response', function(proxy_response) { - proxy_response.addListener('data', function(chunk) { - response.write(chunk, 'binary'); - }); - proxy_response.addListener('end', function() { - response.end(); - }); - response.writeHead(proxy_response.statusCode, proxy_response.headers); - }); - request.addListener('data', function(chunk) { - proxy_request.write(chunk, 'binary'); - }); - request.addListener('end', function() { - proxy_request.end(); - }); -}).listen(8080); - -update_blacklist(); -update_iplist(); \ No newline at end of file diff --git a/test/test-proxy.mjs b/test/test-proxy.mjs new file mode 100644 index 0000000..2971922 --- /dev/null +++ b/test/test-proxy.mjs @@ -0,0 +1,143 @@ +import http from 'http'; +import fs from 'fs'; + +/** + * Description placeholder + * @date 6/30/2023 - 1:26:51 PM + * + * @type {{}} + */ +let blacklist = []; +/** + * Description placeholder + * @date 6/30/2023 - 1:26:51 PM + * + * @type {{}} + */ +let iplist = []; + +fs.watchFile('./blacklist', function(c, p) { + updateBlacklist(); +}); +fs.watchFile('./iplist', function(c, p) { + updateIPlist(); +}); + +/** + * Description placeholder + * @date 6/30/2023 - 1:26:29 PM + */ +function updateBlacklist() { + console.log('Updating blacklist.'); + blacklist = fs.readFileSync('./blacklist', {encoding: 'utf-8'}).split('\n') + .filter(function(rx) { + return rx.length; + }) + .map(function(rx) { + return RegExp(rx); + }); +} + +/** + * Description placeholder + * @date 6/30/2023 - 1:26:51 PM + */ +function updateIPlist() { + console.log('Updating iplist.'); + iplist = fs.readFileSync('./iplist', {encoding: 'utf-8'}).split('\n') + .filter(function(rx) { + return rx.length; + }); +} + +/** + * Description placeholder + * @date 6/30/2023 - 1:26:51 PM + * + * @param {*} ip + * @return {boolean} + */ +function ipAllowed(ip) { + for (i in iplist) { + if (iplist[i] == ip) { + return true; + } + } + return false; +} + +/** + * Description placeholder + * @date 6/30/2023 - 1:26:51 PM + * + * @param {*} host + * @return {boolean} + */ +function hostAllowed(host) { + for (i in blacklist) { + if (blacklist[i].test(host)) { + return false; + } + } + return true; +} + +/** + * Description placeholder + * @date 6/30/2023 - 1:26:51 PM + * + * @param {*} response + * @param {*} msg + */ +function deny(response, msg) { + response.writeHead(401); + response.write(msg); + response.end(); +} + +http.createServer(function(request, response) { + const ip = request.socket.remoteAddress; + if (!ipAllowed(ip)) { + const msg = `IP ${ip} is not allowed to use this proxy`; + deny(response, msg); + console.log(msg); + return; + } + + if (!hostAllowed(request.url)) { + const msg = `Host ${request.url} has been denied by proxy configuration`; + deny(response, msg); + console.log(msg); + return; + } + + console.log(ip + ': ' + request.method + ' ' + request.url); + const agent = new http.Agent({host: request.headers['host'], + port: 80, maxSockets: 1}); + const proxyRequest = http.request({ + host: request.headers['host'], + port: 80, + method: request.method, + path: request.url, + headers: request.headers, + agent: agent, + }); + proxyRequest.addListener('response', function(proxyResponse) { + proxyResponse.addListener('data', function(chunk) { + response.write(chunk, 'binary'); + }); + proxyResponse.addListener('end', function() { + response.end(); + }); + response.writeHead(proxyResponse.statusCode, proxyResponse.headers); + }); + request.addListener('data', function(chunk) { + proxyRequest.write(chunk, 'binary'); + }); + request.addListener('end', function() { + proxyRequest.end(); + }); +}).listen(8080); + +updateBlacklist(); +updateIPlist();