summaryrefslogtreecommitdiffstats
path: root/test/fetch/resource-timing.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-21 20:56:19 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-21 20:56:19 +0000
commit0b6210cd37b68b94252cb798598b12974a20e1c1 (patch)
treee371686554a877842d95aa94f100bee552ff2a8e /test/fetch/resource-timing.js
parentInitial commit. (diff)
downloadnode-undici-upstream.tar.xz
node-undici-upstream.zip
Adding upstream version 5.28.2+dfsg1+~cs23.11.12.3.upstream/5.28.2+dfsg1+_cs23.11.12.3upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--test/fetch/resource-timing.js72
1 files changed, 72 insertions, 0 deletions
diff --git a/test/fetch/resource-timing.js b/test/fetch/resource-timing.js
new file mode 100644
index 0000000..d266f28
--- /dev/null
+++ b/test/fetch/resource-timing.js
@@ -0,0 +1,72 @@
+'use strict'
+
+const { test } = require('tap')
+const { createServer } = require('http')
+const { nodeMajor, nodeMinor } = require('../../lib/core/util')
+const { fetch } = require('../..')
+
+const {
+ PerformanceObserver,
+ performance
+} = require('perf_hooks')
+
+const skip = nodeMajor < 18 || (nodeMajor === 18 && nodeMinor < 2)
+
+test('should create a PerformanceResourceTiming after each fetch request', { skip }, (t) => {
+ t.plan(8)
+
+ const obs = new PerformanceObserver(list => {
+ const expectedResourceEntryName = `http://localhost:${server.address().port}/`
+
+ const entries = list.getEntries()
+ t.equal(entries.length, 1)
+ const [entry] = entries
+ t.same(entry.name, expectedResourceEntryName)
+ t.strictSame(entry.entryType, 'resource')
+
+ t.ok(entry.duration >= 0)
+ t.ok(entry.startTime >= 0)
+
+ const entriesByName = list.getEntriesByName(expectedResourceEntryName)
+ t.equal(entriesByName.length, 1)
+ t.strictSame(entriesByName[0], entry)
+
+ obs.disconnect()
+ performance.clearResourceTimings()
+ })
+
+ obs.observe({ entryTypes: ['resource'] })
+
+ const server = createServer((req, res) => {
+ res.end('ok')
+ }).listen(0, async () => {
+ const body = await fetch(`http://localhost:${server.address().port}`)
+ t.strictSame('ok', await body.text())
+ })
+
+ t.teardown(server.close.bind(server))
+})
+
+test('should include encodedBodySize in performance entry', { skip }, (t) => {
+ t.plan(4)
+ const obs = new PerformanceObserver(list => {
+ const [entry] = list.getEntries()
+ t.equal(entry.encodedBodySize, 2)
+ t.equal(entry.decodedBodySize, 2)
+ t.equal(entry.transferSize, 2 + 300)
+
+ obs.disconnect()
+ performance.clearResourceTimings()
+ })
+
+ obs.observe({ entryTypes: ['resource'] })
+
+ const server = createServer((req, res) => {
+ res.end('ok')
+ }).listen(0, async () => {
+ const body = await fetch(`http://localhost:${server.address().port}`)
+ t.strictSame('ok', await body.text())
+ })
+
+ t.teardown(server.close.bind(server))
+})