Skip to content

Commit

Permalink
Merge pull request #946 from michaelgoin/unidici-tests
Browse files Browse the repository at this point in the history
Unidici tests
  • Loading branch information
michaelgoin authored Oct 11, 2021
2 parents 8396059 + e607b0d commit 7f1d162
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
5 changes: 4 additions & 1 deletion test/versioned/undici/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
"requests.tap.js"
]
}
]
],
"engines": {
"node": ">=12"
}
}
99 changes: 98 additions & 1 deletion test/versioned/undici/requests.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const tap = require('tap')
const { DESTINATIONS } = require('../../../lib/config/attribute-filter')
const helper = require('../../lib/agent_helper')
const metrics = require('../../lib/metrics_helper')
const http = require('http')
const https = require('https')

tap.test('Undici request tests', (t) => {
t.autoend()
Expand Down Expand Up @@ -62,6 +64,67 @@ tap.test('Undici request tests', (t) => {
})
})

t.test('should add HTTP port to segment name when provided', (t) => {
const server = http.createServer((req, res) => {
req.resume()
res.end('http')
})

t.teardown(() => {
server.close()
})

server.listen(0)

helper.runInTransaction(agent, async (transaction) => {
const { port } = server.address()
await undici.request(`http://localhost:${port}`)

metrics.assertSegments(transaction.trace.root, [`External/localhost:${port}/`], {
exact: false
})

transaction.end()
t.end()
})
})

t.test('should add HTTPS port to segment name when provided', async (t) => {
const [key, cert, ca] = await helper.withSSL()
const server = https.createServer({ key, cert }, (req, res) => {
res.write('SSL response')
res.end()
})

t.teardown(() => {
server.close()
})

server.listen(0)

await helper.runInTransaction(agent, async (transaction) => {
const { port } = server.address()

const client = new undici.Client(`https://localhost:${port}`, {
tls: {
ca
}
})

t.teardown(() => {
client.close()
})

await client.request({ path: '/', method: 'GET' })

metrics.assertSegments(transaction.trace.root, [`External/localhost:${port}/`], {
exact: false
})

transaction.end()
})
})

t.test('should add attributes to external segment', (t) => {
helper.runInTransaction(agent, async (tx) => {
const { statusCode } = await undici.request('https://httpbin.org', {
Expand Down Expand Up @@ -154,6 +217,41 @@ tap.test('Undici request tests', (t) => {
})
})

t.test('segments should end on error', (t) => {
const socketEndServer = http.createServer(function badHandler(req) {
req.socket.end()
})

t.teardown(() => {
socketEndServer.close()
})

socketEndServer.listen(0)

helper.runInTransaction(agent, async (transaction) => {
const { port } = socketEndServer.address()
const req = undici.request(`http://localhost:${port}`)

try {
await req
} catch (error) {
metrics.assertSegments(transaction.trace.root, [`External/localhost:${port}/`], {
exact: false
})

const segments = transaction.trace.root.children
const segment = segments[segments.length - 1]

t.ok(segment.timer.start, 'should have started')
t.ok(segment.timer.hasEnd(), 'should have ended')

transaction.end()

t.end()
}
})
})

t.test('400 status', (t) => {
helper.runInTransaction(agent, async (tx) => {
const { statusCode } = await undici.request('https://httpbin.org', {
Expand Down Expand Up @@ -201,7 +299,6 @@ tap.test('Undici request tests', (t) => {
})

t.test('pipeline', (t) => {
// eslint-disable-next-line node/no-unsupported-features/node-builtins
const { pipeline, PassThrough, Readable, Writable } = require('stream')
helper.runInTransaction(agent, async (tx) => {
pipeline(
Expand Down

0 comments on commit 7f1d162

Please sign in to comment.