From 0bbd430c460a3af680485db97d111150276b7d4d Mon Sep 17 00:00:00 2001 From: Marak Date: Tue, 4 Oct 2016 21:47:02 -0400 Subject: [PATCH] [tests] Added multi-language tests #6 * Includes `microservice-examples` package * Adds "hello world" tests for all languages --- package.json | 1 + test/all-languages-tests.js | 64 +++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 test/all-languages-tests.js diff --git a/package.json b/package.json index 20cd10d..d216806 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "devDependencies": { "express": "^4.14.0", "request": "^2.75.0", + "microservice-examples": "3.0.0" "tape": "^4.6.0" } } diff --git a/test/all-languages-tests.js b/test/all-languages-tests.js new file mode 100644 index 0000000..8a203fe --- /dev/null +++ b/test/all-languages-tests.js @@ -0,0 +1,64 @@ +// all-languages-tests.js +var test = require("tape"); +var express = require('express'); +var request = require('request'); + +var stack, handler, app, server, examples; + +stack = require('../'); + +// Remark: babel and coffee-script are commented out since they aren't included in the package +// Even as devDependencies they are too big +var languages = ['bash', /* 'babel', 'coffee-script', */ 'smalltalk', 'lua', 'javascript', 'perl', 'php', 'python', 'python3', 'ruby', 'scheme', 'tcl']; + +test('attempt to require microservice-examples module', function (t) { + examples = require('microservice-examples'); + t.equal(typeof examples.services, "object", "returned services object"); + t.end(); +}); + +test('check if examples are available for all languages', function (t) { + languages.forEach(function (lang) { + t.equal(true, Object.keys(examples.services).indexOf(lang + '-hello-world') > 0, 'found example service for ' + lang); + }); + t.end(); +}); + +test('attempt to start server with handlers for all languages', function (t) { + app = express(); + app.use(stack.plugins.bodyParser()); + languages.forEach(function (lang) { + var service = examples.services[lang + '-hello-world']; + var handler = stack.spawn({ + language: lang, + code: service.code + }); + app.use('/' + lang, handler); + t.equal(typeof handler, "function", "/" + lang + " HTTP endpoint added"); + }); + server = app.listen(3000, function () { + t.end(); + }); +}); + +test('attempt to run hello world all languages', function (t) { + t.plan(languages.length); + languages.forEach(function (lang) { + request('http://localhost:3000/' + lang, function (err, res, body) { + var noCarriageReturn = ["perl", "scheme", "php"]; + if (noCarriageReturn.indexOf(lang) !== -1) { + t.equal(body, 'hello world', 'got correct response from ' + lang); + } else { + t.equal(body, 'hello world\n', 'got correct response from ' + lang); + } + }); + }); +}); +// TODO: request params test with JSON / language specific output + +test('attempt to end server', function (t) { + server.close(function(){ + t.ok(true, "ended server"); + t.end(); + }); +}); \ No newline at end of file