Skip to content

Commit

Permalink
add healthz endpoint and test
Browse files Browse the repository at this point in the history
  • Loading branch information
jchartrand committed May 27, 2024
1 parent e3f44e3 commit 72c2e8e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import verifyAuthHeader from './verifyAuthHeader.js'
import { getConfig } from './config.js'
import testVC from './testVC.js';
import CoordinatorException from './CoordinatorException.js';
import { getSignedDIDAuth, verifyDIDAuth } from './didAuth.js';
import { getSignedDIDAuth } from './didAuth.js';
import { getDataForExchangeSetupPost } from './test-fixtures/vc.js';

async function callService(endpoint, body) {

Expand All @@ -33,6 +34,41 @@ export async function build(opts = {}) {
app.use(express.urlencoded({ extended: false }));
app.use(cors())

app.get('/healthz', async function(req, res) {
const baseURL = `${req.protocol}://${req.headers.host}`
const testData = getDataForExchangeSetupPost('test')
const exchangeURL = `${baseURL}/exchange/setup`
try {
const response = await axios.post(exchangeURL, testData)
const { data: walletQuerys } = response
const walletQuery = walletQuerys.find((q) => q.retrievalId === 'someId')
const parsedDeepLink = new URL(walletQuery.directDeepLink)
const requestURI = parsedDeepLink.searchParams.get('vc_request_url')
const challenge = parsedDeepLink.searchParams.get('challenge')
const didAuth = await getSignedDIDAuth('did:ex:223234', challenge)
const { data: vc } = await axios.post(requestURI, didAuth)

if (
!vc.proof
) {
throw new TransactionException(
503,
'transaction-service healthz failed'
)
}
} catch (e) {
console.log(`exception in healthz: ${JSON.stringify(e)}`)
return res.status(503).json({
error: `transaction-service healthz check failed with error: ${e}`,
healthy: false
})
}
res.send({
message: 'transaction-service server status: ok.',
healthy: true
})
})

app.get('/', async function (req, res, next) {
if (enableStatusService) {
try {
Expand Down
24 changes: 24 additions & 0 deletions src/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,28 @@ describe('api', () => {
})


describe('GET /healthz', function () {
it.only('returns 200 if healthy', async function () {
const response = await request(app).get('/healthz')

expect(response.header['content-type']).to.have.string('json')
expect(response.status).to.eql(200)
expect(response.body).to.eql({
message: 'transaction-service server status: ok.',
healthy: true
})
})

it('returns 503 if not healthy', async function () {
// we delete the keyv store to force an error
//clearKeyv()
const response = await request(app).get('/healthz')

expect(response.header['content-type']).to.have.string('json')
expect(response.status).to.eql(503)
expect(response.body).to.have.property('healthy', false)
initializeTransactionManager()
})
})

})

0 comments on commit 72c2e8e

Please sign in to comment.