Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 36 additions & 31 deletions src/core/__tests__/end.points.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,75 +16,80 @@ afterEach(done => {
})
})

const getActuatorEndpoints = async () => {
const response = await request(server).get('/').accept('application/json').expect(200)
return response.body
}

test('adds an environment variable indicating api endpoits route is configured', async () => {
app.get('/', () => {})
expect(process.env.endpointsAPI).toMatch(/http:\/\/.+?:[0-9]+?/)
})

test('gives list of end points for a single route defined on express app', async () => {
app.get('/', () => {})
const res = await request(server).get('/').accept('application/json').expect(200)
const response = generateResponseObject({ '/': ['GET'] })
expect(res.body).toStrictEqual(response)
const endpoints = await getActuatorEndpoints()
const expectedEndpoints = getExpectedActuatorEndpoints({ '/': ['GET'] })
expect(endpoints).toStrictEqual(expectedEndpoints)
})

test('gives list of end points for multiple routes defined on express app', async () => {
app.get('/', () => {})
app.post('/', () => {})
app.get('/ping', () => {})
const res = await request(server).get('/').accept('application/json').expect(200)
const response = generateResponseObject({ '/': ['GET', 'POST'], '/ping': ['GET'] })
expect(res.body).toStrictEqual(response)
const endpoints = await getActuatorEndpoints()
const expectedEndpoints = getExpectedActuatorEndpoints({ '/': ['GET', 'POST'], '/ping': ['GET'] })
expect(endpoints).toStrictEqual(expectedEndpoints)
})

test('gives list of end points for a multiple routes defined on multiple routers', async () => {
const userRouter = express.Router()
userRouter.get('/', function (req, res) {})
userRouter.post('/', function (req, res) {})
userRouter.delete('/', function (req, res) {})
userRouter.put('/', function (req, res) {})
userRouter.get('/', function () {})
userRouter.post('/', function () {})
userRouter.delete('/', function () {})
userRouter.put('/', function () {})

const productRouter = express.Router()
productRouter.get('/', function (req, res) {})
productRouter.post('/', function (req, res) {})
productRouter.get('/', function () {})
productRouter.post('/', function () {})

app.use('/user', userRouter)
app.use('/product', productRouter)

const res = await request(server).get('/').accept('application/json').expect(200)
const response = generateResponseObject({ '/product': ['GET', 'POST'], '/user': ['DELETE', 'GET', 'POST', 'PUT'] })
expect(res.body).toStrictEqual(response)
const endpoints = await getActuatorEndpoints()
const expectedEndpoints = getExpectedActuatorEndpoints({ '/product': ['GET', 'POST'], '/user': ['DELETE', 'GET', 'POST', 'PUT'] })
expect(endpoints).toStrictEqual(expectedEndpoints)
})

test('coverts express route variable syntax to spring request mapping syntax', async () => {
test('coverts express route variable syntax to spring boot actuator syntax', async () => {
app.get('/:id', () => {})
const res = await request(server).get('/').accept('application/json').expect(200)
const response = generateResponseObject({ '/{id}': ['GET'] })
expect(res.body).toStrictEqual(response)
const endpoints = await getActuatorEndpoints()
const expectedEndpoints = getExpectedActuatorEndpoints({ '/{id}': ['GET'] })
expect(endpoints).toStrictEqual(expectedEndpoints)
})

test('coverts express route multiple variable syntax to spring request mapping syntax', async () => {
test('coverts express route multiple variable syntax to spring boot actuator syntax', async () => {
app.get('/:store/:id', () => {})
const res = await request(server).get('/').accept('application/json').expect(200)
const response = generateResponseObject({ '/{store}/{id}': ['GET'] })
expect(res.body).toStrictEqual(response)
const endpoints = await getActuatorEndpoints()
const expectedEndpoints = getExpectedActuatorEndpoints({ '/{store}/{id}': ['GET'] })
expect(endpoints).toStrictEqual(expectedEndpoints)
})

test('converts express route with regex path parameter to spring request mapping syntax', async () => {
test('converts express route with regex path parameter to spring boot actuator syntax', async () => {
app.get('/api/orders/:orderId([0-9])/items/:itemId([0-9a-f]{24})', () => { });
const res = await request(server).get('/').accept('application/json').expect(200)
const response = generateResponseObject({ '/api/orders/{orderId}/items/{itemId}': ['GET'] })
expect(res.body).toStrictEqual(response)
const endpoints = await getActuatorEndpoints()
const expectedEndpoints = getExpectedActuatorEndpoints({ '/api/orders/{orderId}/items/{itemId}': ['GET'] })
expect(endpoints).toStrictEqual(expectedEndpoints)
})

test('removes escaped dot and backslash in url', async () => {
app.get('/v1\\.0/:store/:id', () => {})
const res = await request(server).get('/').accept('application/json').expect(200)
const response = generateResponseObject({ '/v1.0/{store}/{id}': ['GET'] })
expect(res.body).toStrictEqual(response)
const endpoints = await getActuatorEndpoints()
const expectedEndpoints = getExpectedActuatorEndpoints({ '/v1.0/{store}/{id}': ['GET'] })
expect(endpoints).toStrictEqual(expectedEndpoints)
})

function generateResponseObject(endPoints: { [key: string]: string[] }) {
function getExpectedActuatorEndpoints(endPoints: { [key: string]: string[] }) {
const structure = {
contexts: {
application: {
Expand Down