|
| 1 | +/* |
| 2 | + * Copyright 2022 New Relic Corporation. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict' |
| 7 | + |
| 8 | +const tap = require('tap') |
| 9 | +const helpers = require('./helpers') |
| 10 | +const utils = require('@newrelic/test-utilities') |
| 11 | +const NEXT_TRANSACTION_PREFIX = 'WebTransaction/WebFrameworkUri/Nextjs/GET/' |
| 12 | +const DESTINATIONS = { |
| 13 | + NONE: 0x00, |
| 14 | + TRANS_EVENT: 0x01, |
| 15 | + TRANS_TRACE: 0x02, |
| 16 | + ERROR_EVENT: 0x04, |
| 17 | + BROWSER_EVENT: 0x08, |
| 18 | + SPAN_EVENT: 0x10, |
| 19 | + TRANS_SEGMENT: 0x20 |
| 20 | +} |
| 21 | + |
| 22 | +tap.Test.prototype.addAssert('nextCLMAttrs', 1, function ({ segments, clmEnabled }) { |
| 23 | + segments.forEach(({ segment, name, filepath }) => { |
| 24 | + const attrs = segment.getAttributes() |
| 25 | + if (clmEnabled) { |
| 26 | + this.match( |
| 27 | + attrs, |
| 28 | + { |
| 29 | + 'code.function': name, |
| 30 | + 'code.filepath': filepath |
| 31 | + }, |
| 32 | + 'should add code.function and code.filepath when CLM is enabled.' |
| 33 | + ) |
| 34 | + } else { |
| 35 | + this.notOk(attrs['code.function'], 'should not add code.function when CLM is disabled.') |
| 36 | + this.notOk(attrs['code.filepath'], 'should not add code.filepath when CLM is disabled.') |
| 37 | + } |
| 38 | + }) |
| 39 | +}) |
| 40 | + |
| 41 | +tap.test('Next.js', (t) => { |
| 42 | + t.autoend() |
| 43 | + let agent |
| 44 | + let server |
| 45 | + |
| 46 | + t.before(async () => { |
| 47 | + await helpers.build(__dirname, 'app-dir') |
| 48 | + |
| 49 | + agent = utils.TestAgent.makeInstrumented({ |
| 50 | + attributes: { |
| 51 | + include: ['request.parameters.*'] |
| 52 | + } |
| 53 | + }) |
| 54 | + helpers.registerInstrumentation(agent) |
| 55 | + |
| 56 | + // TODO: would be nice to run a new server per test so there are not chained failures |
| 57 | + // but currently has issues. Potentially due to module caching. |
| 58 | + server = await helpers.start(__dirname, 'app-dir', '3002') |
| 59 | + }) |
| 60 | + |
| 61 | + t.teardown(async () => { |
| 62 | + await server.close() |
| 63 | + agent.unload() |
| 64 | + }) |
| 65 | + |
| 66 | + // since we setup agent in before we need to remove |
| 67 | + // the transactionFinished listener between tests to avoid |
| 68 | + // context leaking |
| 69 | + function setupTransactionHandler(t) { |
| 70 | + return new Promise((resolve) => { |
| 71 | + function txHandler(transaction) { |
| 72 | + resolve(transaction) |
| 73 | + } |
| 74 | + |
| 75 | + agent.agent.on('transactionFinished', txHandler) |
| 76 | + |
| 77 | + t.teardown(() => { |
| 78 | + agent.agent.removeListener('transactionFinished', txHandler) |
| 79 | + }) |
| 80 | + }) |
| 81 | + } |
| 82 | + |
| 83 | + t.test('should capture query params for static, non-dynamic route, page', async (t) => { |
| 84 | + const prom = setupTransactionHandler(t) |
| 85 | + |
| 86 | + const res = await helpers.makeRequest('/static/standard?first=one&second=two', 3002) |
| 87 | + t.equal(res.statusCode, 200) |
| 88 | + const tx = await prom |
| 89 | + |
| 90 | + const agentAttributes = getTransactionEventAgentAttributes(tx) |
| 91 | + |
| 92 | + t.match(agentAttributes, { |
| 93 | + 'request.parameters.first': 'one', |
| 94 | + 'request.parameters.second': 'two' |
| 95 | + }) |
| 96 | + t.equal(tx.name, `${NEXT_TRANSACTION_PREFIX}/static/standard`) |
| 97 | + }) |
| 98 | + |
| 99 | + t.test('should capture query and route params for static, dynamic route, page', async (t) => { |
| 100 | + const prom = setupTransactionHandler(t) |
| 101 | + |
| 102 | + const res = await helpers.makeRequest('/static/dynamic/testing?queryParam=queryValue', 3002) |
| 103 | + t.equal(res.statusCode, 200) |
| 104 | + const tx = await prom |
| 105 | + |
| 106 | + const agentAttributes = getTransactionEventAgentAttributes(tx) |
| 107 | + |
| 108 | + t.match(agentAttributes, { |
| 109 | + 'request.parameters.route.value': 'testing', // route [value] param |
| 110 | + 'request.parameters.queryParam': 'queryValue' |
| 111 | + }) |
| 112 | + |
| 113 | + t.notOk(agentAttributes['request.parameters.route.queryParam']) |
| 114 | + t.equal(tx.name, `${NEXT_TRANSACTION_PREFIX}/static/dynamic/[value]`) |
| 115 | + }) |
| 116 | + |
| 117 | + t.test( |
| 118 | + 'should capture query params for server-side rendered, non-dynamic route, page', |
| 119 | + async (t) => { |
| 120 | + const prom = setupTransactionHandler(t) |
| 121 | + const res = await helpers.makeRequest('/person/1?first=one&second=two', 3002) |
| 122 | + t.equal(res.statusCode, 200) |
| 123 | + const tx = await prom |
| 124 | + |
| 125 | + const agentAttributes = getTransactionEventAgentAttributes(tx) |
| 126 | + |
| 127 | + t.match( |
| 128 | + agentAttributes, |
| 129 | + { |
| 130 | + 'request.parameters.first': 'one', |
| 131 | + 'request.parameters.second': 'two' |
| 132 | + }, |
| 133 | + 'should match transaction attributes' |
| 134 | + ) |
| 135 | + |
| 136 | + t.notOk(agentAttributes['request.parameters.route.first']) |
| 137 | + t.notOk(agentAttributes['request.parameters.route.second']) |
| 138 | + t.equal(tx.name, `${NEXT_TRANSACTION_PREFIX}/person/[id]`) |
| 139 | + } |
| 140 | + ) |
| 141 | + |
| 142 | + function getTransactionEventAgentAttributes(transaction) { |
| 143 | + return transaction.trace.attributes.get(DESTINATIONS.TRANS_EVENT) |
| 144 | + } |
| 145 | +}) |
0 commit comments