Skip to content

Commit

Permalink
Fix bug in tracking of registered routes. (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgwozdz committed Sep 30, 2022
1 parent 4b74870 commit c5b7329
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
11 changes: 8 additions & 3 deletions packages/koop-core/src/helpers/register-plugin-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function registerProviderRoutes ({ provider, controller, server, pluginRoutes },
const namespace = provider.namespace.replace(/\s/g, '').toLowerCase()
const { hosts, disableIdParam } = provider
const { routePrefix } = options
const pluginRouteMap = {}
const routeMap = {}

pluginRoutes.forEach(route => {
const { path, methods, absolutePath, output } = route
Expand All @@ -27,10 +27,10 @@ function registerProviderRoutes ({ provider, controller, server, pluginRoutes },
})

// For each output plugin, keep track of routes, methods
_.set(pluginRouteMap, `${output}.${routeString}`, methods)
addMethodsToRouteMap(routeMap, `${output}.${routeString}`, methods)
})

return pluginRouteMap
return routeMap
}

function bindController (params) {
Expand All @@ -53,4 +53,9 @@ function registerRoutes (params) {
})
}

function addMethodsToRouteMap (map, path, methods) {
const existingMethods = _.get(map, path, [])
_.set(map, path, _.concat(existingMethods, methods))
}

module.exports = registerProviderRoutes
17 changes: 12 additions & 5 deletions packages/koop-core/src/helpers/register-provider-routes.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
const path = require('path')
const _ = require('lodash')
const validateHttpMethods = require('./validate-http-methods')

const composePath = (routePrefix, routeString) => path.posix.join(routePrefix, routeString)

function registerProviderRoutes ({ provider, controller, server }, options = {}) {
const { routePrefix = '' } = options
const { routes = [], namespace } = provider
const providerRoutes = {}
const routeMap = {}

routes.forEach(route => {
const { path, methods } = route
validateHttpMethods(methods)

const composedPath = composePath(routePrefix, path)
const routePath = composePath(routePrefix, path)

registerRoutes({
server,
path: composedPath,
path: routePath,
methods,
controller: bindController({ controller, route, namespace })
})
providerRoutes[composedPath] = methods
addMethodsToRouteMap(routeMap, routePath, methods)
})

return providerRoutes
return routeMap
}

function bindController (params) {
Expand All @@ -45,4 +47,9 @@ function registerRoutes (params) {
})
}

function addMethodsToRouteMap (map, path, methods) {
const existingMethods = _.get(map, path, [])
_.set(map, path, _.concat(existingMethods, methods))
}

module.exports = registerProviderRoutes
19 changes: 12 additions & 7 deletions packages/koop-core/test/helpers/register-plugin-routes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ require('should-sinon')
const registerPluginRoutes = require('../../src/helpers/register-plugin-routes')

const mockController = {
testHandler: () => {}
testHandler: () => {},
testHandler2: () => {}
}

const mockProvider = {
Expand All @@ -14,13 +15,15 @@ const mockProvider = {
}

const mockPluginRoutes = [
{ output: 'MockOutput', path: '/output-plugin', methods: ['get'], handler: 'testHandler' }
{ output: 'MockOutput', path: '/output-plugin', methods: ['get'], handler: 'testHandler' },
{ output: 'MockOutput', path: '/output-plugin', methods: ['post'], handler: 'testHandler2' }
]

describe('Tests for register-plugin-routes', function () {
it('should register a plugin route, lowercased', () => {
const mockServer = sinon.spy({
get: () => {}
get: () => {},
post: () => {}
})

const pluginRouteMap = registerPluginRoutes({
Expand All @@ -33,18 +36,20 @@ describe('Tests for register-plugin-routes', function () {
mockServer.get.should.be.calledOnce()
pluginRouteMap.should.deepEqual({
MockOutput: {
'/mock-provider/:host/:id/output-plugin': ['get']
'/mock-provider/:host/:id/output-plugin': ['get', 'post']
}
})
})

it('should register a plugin route, uppercased', () => {
const mockPluginRoutes = [
{ output: 'MockOutput', path: '/output-plugin', methods: ['GET'], handler: 'testHandler' }
{ output: 'MockOutput', path: '/output-plugin', methods: ['GET'], handler: 'testHandler' },
{ output: 'MockOutput', path: '/output-plugin', methods: ['POST'], handler: 'testHandler2' }
]

const mockServer = sinon.spy({
get: () => {}
get: () => {},
post: () => {}
})

const pluginRouteMap = registerPluginRoutes({
Expand All @@ -57,7 +62,7 @@ describe('Tests for register-plugin-routes', function () {
mockServer.get.should.be.calledOnce()
pluginRouteMap.should.deepEqual({
MockOutput: {
'/mock-provider/:host/:id/output-plugin': ['GET']
'/mock-provider/:host/:id/output-plugin': ['GET', 'POST']
}
})
})
Expand Down
13 changes: 10 additions & 3 deletions packages/koop-core/test/helpers/register-provider-routes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ require('should-sinon')
const registerProviderRoutes = require('../../src/helpers/register-provider-routes')

const mockController = {
testHandler: () => {}
testHandler: () => {},
testHandler2: () => {}
}

describe('Tests for register-provider-routes, lowercased', function () {
it('should register a provider route', () => {
const mockServer = sinon.spy({
get: () => {}
get: () => {},
post: () => {}
})

const mockProvider = {
Expand All @@ -21,6 +23,11 @@ describe('Tests for register-provider-routes, lowercased', function () {
path: '/test/route',
methods: ['get'],
handler: 'testHandler'
},
{
path: '/test/route',
methods: ['post'],
handler: 'testHandler2'
}]
}
const providerRouteMap = registerProviderRoutes({
Expand All @@ -30,7 +37,7 @@ describe('Tests for register-provider-routes, lowercased', function () {
})

mockServer.get.should.be.calledOnce()
providerRouteMap.should.deepEqual({ '/test/route': ['get'] })
providerRouteMap.should.deepEqual({ '/test/route': ['get', 'post'] })
})

it('should register a provider route, uppercased', () => {
Expand Down

0 comments on commit c5b7329

Please sign in to comment.