Skip to content

Commit

Permalink
test: Updated how we handle the koa-router nuance of wildcard routes
Browse files Browse the repository at this point in the history
  • Loading branch information
bizob2828 committed Sep 18, 2024
1 parent 608dd98 commit 5e76245
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
4 changes: 3 additions & 1 deletion test/versioned/koa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,7 @@
]
}
],
"dependencies": {}
"engines": {
"node": ">=18"
}
}
41 changes: 31 additions & 10 deletions test/versioned/koa/router-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@
*/

'use strict'
const fs = require('fs')

/**
* koa-router and @koa/router updated how they defined wildcard routing
* It used to be native and then relied on `path-to-regexp`. If `path-to-regexp`
* is present get the version. For post 8 it relies on different syntax to define
* routes. If it is not present assume the pre 8 behavior of `path-to-regexp`
* is the same. Also cannot use require because `path-to-regexp` defines exports
* and package.json is not a defined export.
*/
function getPathToRegexpVersion() {
let pathToRegexVersion
try {
;({ version: pathToRegexVersion } = JSON.parse(
fs.readFileSync(`${__dirname}/node_modules/path-to-regexp/package.json`)
))
} catch {
pathToRegexVersion = '6.0.0'
}
return pathToRegexVersion
}

module.exports = (pkg) => {
const tap = require('tap')
Expand All @@ -15,6 +36,7 @@ module.exports = (pkg) => {
tap.test(`${pkg} instrumentation`, (t) => {
const { version: pkgVersion } = require(`${pkg}/package.json`)
const paramMiddlewareName = 'Nodejs/Middleware/Koa/middleware//:first'
const pathToRegexVersion = getPathToRegexpVersion()

/**
* Helper to decide how to name nested route segments
Expand Down Expand Up @@ -149,7 +171,7 @@ module.exports = (pkg) => {
t.test('should name and produce segments for matched wildcard path', (t) => {
const { agent, router, app } = t.context
let path = '(.*)'
if (pkg === 'koa-router' && semver.gte(pkgVersion, '13.0.1')) {
if (semver.gte(pathToRegexVersion, '8.0.0')) {
path = '{*any}'
}
router.get(`/:first/${path}`, function firstMiddleware(ctx) {
Expand Down Expand Up @@ -347,16 +369,15 @@ module.exports = (pkg) => {
ctx.body = ' second'
})

const segmentTree =
pkg === 'koa-router' && semver.gte(pkgVersion, '13.0.1')
? ['Nodejs/Middleware/Koa/terminalMiddleware//:second']
: [
'Nodejs/Middleware/Koa/secondMiddleware//:first',
[
'Nodejs/Middleware/Koa/secondMiddleware//:second',
['Nodejs/Middleware/Koa/terminalMiddleware//:second']
]
const segmentTree = semver.gte(pathToRegexVersion, '8.0.0')
? ['Nodejs/Middleware/Koa/terminalMiddleware//:second']
: [
'Nodejs/Middleware/Koa/secondMiddleware//:first',
[
'Nodejs/Middleware/Koa/secondMiddleware//:second',
['Nodejs/Middleware/Koa/terminalMiddleware//:second']
]
]
app.use(router.routes())
agent.on('transactionFinished', (tx) => {
t.assertSegments(tx.trace.root, [
Expand Down

0 comments on commit 5e76245

Please sign in to comment.