Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Updated how we handle the koa-router nuance of wildcard routes #2588

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
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
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
}
Comment on lines +17 to +27
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely ridiculous that this is necessary.


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
Loading