From 31504d02138cec59769877564b96f2de3ad826dd Mon Sep 17 00:00:00 2001 From: leftieFriele Date: Fri, 7 Jul 2023 10:18:07 +0200 Subject: [PATCH] adding /ping for eik-client ping command --- lib/main.js | 8 ++++++-- test/ping.js | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 test/ping.js diff --git a/lib/main.js b/lib/main.js index 9e2413b8..369312be 100644 --- a/lib/main.js +++ b/lib/main.js @@ -195,6 +195,11 @@ const EikService = class EikService { // Routes // + const pingRoute = async (request, reply) => { + reply.type('text/plain') + reply.code(200) + reply.send('pong') + } const authPostRoutes = async (request, reply) => { const outgoing = await this._authPost.handler( request.raw, @@ -302,7 +307,6 @@ const EikService = class EikService { reply.redirect(outgoing.location); }; - const aliasGetRoute = async (request, reply) => { const params = utils.sanitizeParameters(request.raw.url); const outgoing = await this._aliasGet.handler( @@ -363,6 +367,7 @@ const EikService = class EikService { reply.send(outgoing.body); }; + app.get('/ping', pingRoute); // // Authentication @@ -372,7 +377,6 @@ const EikService = class EikService { app.post(`/${eik.prop.base_auth}/login`, authPostRoutes); - // // Packages // diff --git a/test/ping.js b/test/ping.js new file mode 100644 index 00000000..8167109b --- /dev/null +++ b/test/ping.js @@ -0,0 +1,25 @@ +import Fastify from 'fastify'; +import tap from 'tap'; +import fetch from 'node-fetch' + +import Sink from "@eik/core/lib/sinks/test.js"; +import Server from '../lib/main.js'; + +tap.test('ping - returns pong', async t => { + const service = new Server({ customSink: new Sink() }); + + const app = Fastify({ + ignoreTrailingSlash: true, + }); + app.register(service.api()); + + const address = await app.listen({ port: 0, host: '127.0.0.1' }); + const response = await fetch(`${address}/ping`, { + method: 'GET', + }); + t.ok(response.ok) + t.equal(response.status, 200, 'returns 200 OK') + t.equal(await response.text(), 'pong') + + await app.close() +})