Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bug 527304 - Implement the /about and /whoami endpoints - about endpoint
  • Loading branch information
mrennie committed Nov 15, 2017
1 parent 00579f0 commit 733cb09
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 0 deletions.
3 changes: 3 additions & 0 deletions modules/orionode/index.js
Expand Up @@ -66,6 +66,7 @@ function startServer(options) {
addModulePath.addPath(path.join(__dirname, modulePath));
});
}

options.maxAge = typeof options.maxAge === "number" ? options.maxAge : undefined;
var contextPath = options.configParams["orion.context.path"] || "";
var listenContextPath = options.configParams["orion.context.listenPath"] || false;
Expand Down Expand Up @@ -143,6 +144,8 @@ function startServer(options) {
let CloudFoundry = require('./lib/cf').CloudFoundry;
app.use('/metrics', require('./lib/metrics').router(options));
app.use('/version', require('./lib/version').router(options));
let About = require('./lib/about').About;
app.use('/about', new About().createRouter(options));
loadEndpoints(true);
app.use(require('./lib/user').router(options));
app.use('/site', options.authenticate, checkAuthenticated, checkAccessRights, require('./lib/sites')(options));
Expand Down
66 changes: 66 additions & 0 deletions modules/orionode/lib/about.js
@@ -0,0 +1,66 @@
/*******************************************************************************
* Copyright (c) 2016, 2017 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
/*eslint-env node */
var express = require('express'),
api = require('./api'),
responseTime = require('response-time'),
pjson = require('../package.json');

var XHTML_1 = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<title>",
XHTML_2 = "</title>\n <style type = \"text/css\"> td { padding-right : 10px; }</style></head>\n<body>\n",
XHTML_3 = "</body>\n</html>",
BUILD_ID = "unknown";

class About {
/**
* @description Create a new instance of the class
*/
constructor() {

}
/**
* @description Create an express Router for handling /about
* @param {?} options The map of options. Must contain the configParams property
* @returns {Router} A new express router for handling /about
* @throws {Error} If options.configParams is not defined
* @since 17.0
*/
createRouter(options) {
var configParams = options.configParams;
if (!configParams) {
throw new Error('options.configParams is required');
}
return express.Router()
.use(responseTime({digits: 2, header: "X-About-Response-Time", suffix: true}))
.get('*', /* @callback */ function (req, res) {
var about = String(XHTML_1).concat("About").concat(XHTML_2);
var buildID = configParams["orion.buildId"] || BUILD_ID;
if(buildID === "unknown" && pjson && typeof pjson.version === 'string') {
//for the NPM case we want to return the version published to NPM (from the package.json)
buildID = pjson.version;
}
about = about.concat("<table><tr><td><img src=\"../webapp/orion-96.png\"/></td><td><p>");
about = about.concat("Build Id: " + buildID + "<br/></p></td></tr></table>");
about = about.concat("<table>");
about = about.concat("<tr><th align=\"left\">Library</th><th align=\"center\">Version</th></tr>");
if(pjson.dependencies) {
Object.keys(pjson.dependencies).forEach(function(key) {
about = about.concat("<tr><td align=\"left\">"+key+"</td><td align=\"center\">"+pjson.dependencies[key]+"</td></tr>");
});
}
about = about.concat("</table>").concat(XHTML_3);
res.contentType('html');
return api.writeResponse(200, res, null, about);
});
}
}

module.exports.About = About;
57 changes: 57 additions & 0 deletions modules/orionode/test/endpoints/test-about.js
@@ -0,0 +1,57 @@
/*******************************************************************************
* Copyright (c) 2017 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
/*eslint-env mocha */
var assert = require('assert'),
testData = require("../support/test_data"),
testHelper = require('../support/testHelper');

var CONTEXT_PATH = testHelper.CONTEXT_PATH,
PREFIX = CONTEXT_PATH + '/about';

var request = testData.setupOrionServer();

describe("About endpoint", function() {
it("testAbout", function(done) {
request()
.get(PREFIX)
.expect(200)
.end(function(err, res) {
testHelper.throwIfError(err);
assert(res.body, "There sould be a body in the response.");
done();
});
});
it("testAbout/about", function(done) {
request()
.get(PREFIX+PREFIX)
.expect(200)
.end(function(err, res) {
testHelper.throwIfError(err);
assert(res.body, "There sould be a body in the response.");
done();
});
});
it("testAbout.html", function(done) {
request()
.get(PREFIX+'.html')
.expect(404, done);
});
it("testAbout/about.html", function(done) {
request()
.get(PREFIX+PREFIX+'/html')
.expect(200)
.end(function(err, res) {
testHelper.throwIfError(err);
assert(res.body, "There sould be a body in the response.");
done();
});
});
});
44 changes: 44 additions & 0 deletions modules/orionode/test/endpoints/test-version.js
@@ -0,0 +1,44 @@
/*******************************************************************************
* Copyright (c) 2017 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
/*eslint-env mocha */
var assert = require('assert'),
testData = require("../support/test_data"),
testHelper = require('../support/testHelper');

var CONTEXT_PATH = testHelper.CONTEXT_PATH,
PREFIX = CONTEXT_PATH + '/version';

var request = testData.setupOrionServer();

describe("Version endpoint", function() {
it("testVersion", function(done) {
request()
.get(PREFIX)
.expect(200)
.end(function(err, res) {
testHelper.throwIfError(err);
assert(res.body, "There sould be a body in the response.");
assert(res.body.build, "There should be a build in the body;");
done();
});
});
it("testVersion/version", function(done) {
request()
.get(PREFIX+PREFIX)
.expect(200)
.end(function(err, res) {
testHelper.throwIfError(err);
assert(res.body, "There sould be a body in the response.");
assert(res.body.build, "There should be a build in the body;");
done();
});
});
});

0 comments on commit 733cb09

Please sign in to comment.