From 8beb4b8876099a98d67868c3dbfd31492a969c7d Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Fri, 6 Jan 2017 15:58:02 +0100 Subject: [PATCH] Add simple API resource #191 --- lib/routes/api.js | 19 +++++++++++++++++ lib/routes/index.js | 1 + test/unit/routes/api.test.js | 41 ++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 lib/routes/api.js create mode 100644 test/unit/routes/api.test.js diff --git a/lib/routes/api.js b/lib/routes/api.js new file mode 100644 index 0000000000..272018ffcd --- /dev/null +++ b/lib/routes/api.js @@ -0,0 +1,19 @@ +'use strict'; + +const apiDef = { + version: 1, + links: { + 'feature-toggles': { uri: '/api/features' }, + 'strategies': { uri: '/api/strategies' }, + 'events': { uri: '/api/events' }, + 'client-register': { uri: '/api/client/register' }, + 'client-metrics': { uri: '/api/client/register' }, + 'seen-toggles': { uri: '/api/client/seen-toggles' }, + }, +}; + +module.exports = (app) => { + app.get('/', (req, res) => { + res.json(apiDef); + }); +}; diff --git a/lib/routes/index.js b/lib/routes/index.js index 5491beaf85..30208efe2c 100644 --- a/lib/routes/index.js +++ b/lib/routes/index.js @@ -2,6 +2,7 @@ exports.createAPI = function (router, config) { + require('./api')(router, config); require('./event')(router, config); require('./feature')(router, config); require('./feature-archive')(router, config); diff --git a/test/unit/routes/api.test.js b/test/unit/routes/api.test.js new file mode 100644 index 0000000000..cb1dbe0994 --- /dev/null +++ b/test/unit/routes/api.test.js @@ -0,0 +1,41 @@ +'use strict'; + +const test = require('ava'); +const store = require('./fixtures/store'); +const supertest = require('supertest'); +const logger = require('../../../lib/logger'); +const getApp = require('../../../lib/app'); + +const { EventEmitter } = require('events'); +const eventBus = new EventEmitter(); + +test.beforeEach(() => { + logger.setLevel('FATAL'); +}); + +function getSetup () { + const base = `/random${Math.round(Math.random() * 1000)}`; + const stores = store.createStores(); + const app = getApp({ + baseUriPath: base, + stores, + eventBus, + }); + + return { + base, + featureToggleStore: stores.featureToggleStore, + request: supertest(app), + }; +} + +test('should get api defintion', t => { + const { request, base } = getSetup(); + return request + .get(`${base}/api/`) + .expect('Content-Type', /json/) + .expect(200) + .expect((res) => { + t.true(res.body.links['feature-toggles'].uri === '/api/features'); + }); +});