-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathtrace-tagging.spec.js
More file actions
93 lines (72 loc) · 2.83 KB
/
Copy pathtrace-tagging.spec.js
File metadata and controls
93 lines (72 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
'use strict'
const assert = require('node:assert/strict')
const path = require('path')
const Axios = require('axios')
const {
sandboxCwd,
useSandbox,
FakeAgent,
spawnProc
} = require('../helpers')
describe('ASM Trace Tagging rules', () => {
let axios, cwd, appFile, agent, proc
function startServer () {
beforeEach(async () => {
agent = await new FakeAgent().start()
const env = {
DD_TRACE_AGENT_PORT: agent.port,
DD_APPSEC_ENABLED: 'true',
DD_APPSEC_RULES: path.join(cwd, 'appsec', 'data-collection', 'data-collection-rules.json')
}
proc = await spawnProc(appFile, { cwd, env, execArgv: [] })
axios = Axios.create({ baseURL: proc.url })
})
afterEach(async () => {
proc.kill()
await agent.stop()
})
}
describe('express', () => {
useSandbox(['express'])
before(() => {
cwd = sandboxCwd()
appFile = path.join(cwd, 'appsec/data-collection/index.js')
})
startServer()
it('should report waf attributes', async () => {
await axios.get('/', { headers: { 'User-Agent': 'TraceTaggingTest/v1' } })
await agent.assertMessageReceived(({ _, payload }) => {
assert.ok(Object.hasOwn(payload[0][0].meta, '_dd.appsec.trace.agent'))
assert.strictEqual(payload[0][0].meta['_dd.appsec.trace.agent'], 'TraceTaggingTest/v1')
assert.ok(Object.hasOwn(payload[0][0].metrics, '_dd.appsec.trace.integer'))
assert.strictEqual(payload[0][0].metrics['_dd.appsec.trace.integer'], 1234)
})
})
})
describe('fastify', () => {
useSandbox(['fastify'])
before(() => {
cwd = sandboxCwd()
appFile = path.join(cwd, 'appsec/data-collection/fastify.js')
})
startServer()
it('should report waf attributes', async () => {
let fastifyRequestReceived = false
await axios.get('/', { headers: { 'User-Agent': 'TraceTaggingTest/v1' } })
// With fastify, the 'fastify.request' is not the first message received by FakeAgent, unlike express.
// That's why expectedMessageCount is set to 10 and the fastifyRequestReceived flag is added to check
// that the ‘fastify.request’ message has been received and checked.
await agent.assertMessageReceived(({ _, payload }) => {
if (payload[0][0].name !== 'fastify.request') {
throw new Error('Not the span we are looking for')
}
fastifyRequestReceived = true
assert.ok(Object.hasOwn(payload[0][0].meta, '_dd.appsec.trace.agent'))
assert.strictEqual(payload[0][0].meta['_dd.appsec.trace.agent'], 'TraceTaggingTest/v1')
assert.ok(Object.hasOwn(payload[0][0].metrics, '_dd.appsec.trace.integer'))
assert.strictEqual(payload[0][0].metrics['_dd.appsec.trace.integer'], 1234)
}, 30000, 10, true)
assert.strictEqual(fastifyRequestReceived, true)
})
})
})