Skip to content

Commit

Permalink
Revert "test: Updated how we handle the koa-router nuance of wildcard…
Browse files Browse the repository at this point in the history
… routes (newrelic#2588)"

This reverts commit 544c9e7.
  • Loading branch information
sumitsuthar committed Sep 20, 2024
1 parent 9679a7a commit 58d0487
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 34 deletions.
4 changes: 1 addition & 3 deletions test/versioned/koa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,5 @@
]
}
],
"engines": {
"node": ">=18"
}
"dependencies": {}
}
41 changes: 10 additions & 31 deletions test/versioned/koa/router-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,6 @@
*/

'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 @@ -36,7 +15,6 @@ 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 @@ -171,7 +149,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 (semver.gte(pathToRegexVersion, '8.0.0')) {
if (pkg === 'koa-router' && semver.gte(pkgVersion, '13.0.1')) {
path = '{*any}'
}
router.get(`/:first/${path}`, function firstMiddleware(ctx) {
Expand Down Expand Up @@ -369,15 +347,16 @@ module.exports = (pkg) => {
ctx.body = ' 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']
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']
]
]
]
app.use(router.routes())
agent.on('transactionFinished', (tx) => {
t.assertSegments(tx.trace.root, [
Expand Down

0 comments on commit 58d0487

Please sign in to comment.