|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { tspl } = require('@matteo.collina/tspl') |
| 4 | +const { describe, test, before, after } = require('node:test') |
| 5 | +const { fetch } = require('../../..') |
| 6 | + |
| 7 | +let diagnosticsChannel |
| 8 | +let skip = false |
| 9 | +try { |
| 10 | + diagnosticsChannel = require('node:diagnostics_channel') |
| 11 | +} catch { |
| 12 | + skip = true |
| 13 | +} |
| 14 | + |
| 15 | +const { createServer } = require('http') |
| 16 | + |
| 17 | +describe('diagnosticsChannel for fetch', { skip }, () => { |
| 18 | + let server |
| 19 | + before(() => { |
| 20 | + server = createServer((req, res) => { |
| 21 | + res.setHeader('Content-Type', 'text/plain') |
| 22 | + res.setHeader('trailer', 'foo') |
| 23 | + res.write('hello') |
| 24 | + res.addTrailers({ |
| 25 | + foo: 'oof' |
| 26 | + }) |
| 27 | + res.end() |
| 28 | + }) |
| 29 | + }) |
| 30 | + |
| 31 | + after(() => { server.close() }) |
| 32 | + |
| 33 | + test('fetch', async t => { |
| 34 | + t = tspl(t, { plan: 17 }) |
| 35 | + |
| 36 | + let startCalled = 0 |
| 37 | + diagnosticsChannel.channel('tracing:undici:fetch:start').subscribe(({ req, input, init, result, error }) => { |
| 38 | + startCalled += 1 |
| 39 | + if (input.redirect) { |
| 40 | + t.strictEqual(input, 'badrequest') |
| 41 | + t.deepStrictEqual(init, { redirect: 'error' }) |
| 42 | + } else { |
| 43 | + t.strictEqual(input, `http://localhost:${server.address().port}`) |
| 44 | + t.deepStrictEqual(init, undefined) |
| 45 | + } |
| 46 | + }) |
| 47 | + |
| 48 | + let endCalled = 0 |
| 49 | + diagnosticsChannel.channel('tracing:undici:fetch:end').subscribe(({ req, input, init, result, error }) => { |
| 50 | + endCalled += 1 |
| 51 | + if (init && init.redirect) { |
| 52 | + t.strictEqual(input, 'badrequest') |
| 53 | + t.deepStrictEqual(init, { redirect: 'error' }) |
| 54 | + } else { |
| 55 | + t.strictEqual(input, `http://localhost:${server.address().port}`) |
| 56 | + t.deepStrictEqual(init, undefined) |
| 57 | + } |
| 58 | + t.strictEqual(result, null) |
| 59 | + }) |
| 60 | + |
| 61 | + let asyncStartCalled = 0 |
| 62 | + diagnosticsChannel.channel('tracing:undici:fetch:asyncStart').subscribe(({ req, input, init, result, error }) => { |
| 63 | + asyncStartCalled += 1 |
| 64 | + if (init && init.redirect) { |
| 65 | + t.strictEqual(input, 'badrequest') |
| 66 | + t.deepStrictEqual(init, { redirect: 'error' }) |
| 67 | + } else { |
| 68 | + t.strictEqual(input, `http://localhost:${server.address().port}`) |
| 69 | + t.deepStrictEqual(init, undefined) |
| 70 | + t.ok(result) |
| 71 | + } |
| 72 | + }) |
| 73 | + |
| 74 | + let asyncEndCalled = 0 |
| 75 | + diagnosticsChannel.channel('tracing:undici:fetch:asyncEnd').subscribe(async ({ req, input, init, result, error }) => { |
| 76 | + asyncEndCalled += 1 |
| 77 | + if (init && init.redirect) { |
| 78 | + t.strictEqual(input, 'badrequest') |
| 79 | + t.deepStrictEqual(init, { redirect: 'error' }) |
| 80 | + t.strictEqual(result, null) |
| 81 | + t.ok(error) |
| 82 | + t.strictEqual(error.cause.code, 'ERR_INVALID_URL') |
| 83 | + } else { |
| 84 | + t.strictEqual(input, `http://localhost:${server.address().port}`) |
| 85 | + t.deepStrictEqual(init, undefined) |
| 86 | + t.ok(result) |
| 87 | + t.strictEqual(result.status, 200) |
| 88 | + t.strictEqual(error, null) |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + server.listen(0, async () => { |
| 93 | + await fetch(`http://localhost:${server.address().port}`) |
| 94 | + try { |
| 95 | + await fetch('badrequest', { redirect: 'error' }) |
| 96 | + } catch (e) { } |
| 97 | + server.close() |
| 98 | + t.strictEqual(startCalled, 1) |
| 99 | + t.strictEqual(endCalled, 1) |
| 100 | + t.strictEqual(asyncStartCalled, 1) |
| 101 | + t.strictEqual(asyncEndCalled, 1) |
| 102 | + }) |
| 103 | + |
| 104 | + await t.completed |
| 105 | + }) |
| 106 | +}) |
0 commit comments