Skip to content

Commit

Permalink
adds bare minimum tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsan committed Apr 24, 2019
1 parent 2a62be7 commit 9a20296
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
50 changes: 28 additions & 22 deletions eureka-middleware.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
const xml2js = require('xml2js')

module.exports = function eurekaMiddleware(params) {
function mapServersFromEurekaResponse (data, params) {
return data
.applications
.application
.filter(service => params.serviceName ? service.name[0] === params.serviceName : true)
.map(service => service.instance)
.reduce((flat, toFlatten) =>
flat.concat(toFlatten), [])
.filter(service => params.serviceVipAddress ? service.vipAddress[0] === params.serviceVipAddress : true)
.map(service => params.protocol || 'http' + '://' + service.ipAddr[0] + ':' + service.port[0]._)
}

function eurekaMiddleware (params) {
params = params || {}

function middleware(options, resilient) {
function middleware (options, resilient) {
options.set('path', params.path || '/eureka/apps')
options.set('gzip', true)

return {
'in': function (err, res, next) {
if (err) return next()
xml2js.parseString(res.body, (err, result) => {
if (err) return next()

try {
res.data = mapServersFromEurekaResponse(result)
} catch (err) {
//fails silently
res.data = mapServersFromEurekaResponse(result, params)
} catch (err) {
// fails silently
} finally {
next()
next()
}
})
},
Expand All @@ -25,20 +39,12 @@ module.exports = function eurekaMiddleware(params) {
}
}
}

function mapServersFromEurekaResponse(data) {
return data
.applications
.application
.filter(service => params.serviceName ? service.name[0] === params.serviceName : true)
.map(service => service.instance)
.reduce((flat, toFlatten) =>
flat.concat(toFlatten), [])
.filter(service => params.serviceVipAddress ? service.vipAddress[0] === params.serviceVipAddress : true)
.map(service => params.protocol || 'http' + '://' + service.ipAddr[0] + ':' + service.port[0]._)
}

middleware.type = 'discovery'


middleware.type = 'discovery'

return middleware
}
}

eurekaMiddleware.mapServersFromEurekaResponse = mapServersFromEurekaResponse

module.exports = eurekaMiddleware
21 changes: 21 additions & 0 deletions test/eureka-middleware.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require('tap').mochaGlobals()

const { expect } = require('chai')

describe('eureka-middleware.js', () => {
it('should export a function', () => {
const eurekaMiddleware = require('../eureka-middleware')
expect(eurekaMiddleware).to.be.instanceof(Function)
})

it('should be named as eurekaMiddleware', () => {
const eurekaMiddleware = require('../eureka-middleware')
expect(eurekaMiddleware).to.have.property('name', 'eurekaMiddleware')
})

it('should export mapServersFromEurekaResponse', () => {
const eurekaMiddleware = require('../eureka-middleware')
expect(eurekaMiddleware).to.have.property('mapServersFromEurekaResponse')
expect(eurekaMiddleware.mapServersFromEurekaResponse).to.be.instanceof(Function)
})
})

0 comments on commit 9a20296

Please sign in to comment.