|
| 1 | +const assert = require('node:assert'); |
| 2 | +const http = require('http'); |
| 3 | +const utils = require('../../utils'); |
| 4 | + |
| 5 | +describe('test/lib/core/fetch_tracer.test.js', () => { |
| 6 | + const version = utils.getNodeVersion(); |
| 7 | + if (version < 20) return; |
| 8 | + |
| 9 | + let app; |
| 10 | + let mockServer; |
| 11 | + |
| 12 | + before(async () => { |
| 13 | + // Create a mock server to capture headers |
| 14 | + mockServer = http.createServer((req, res) => { |
| 15 | + const headers = { |
| 16 | + 'Content-Type': 'application/json', |
| 17 | + }; |
| 18 | + if (req.headers['x-trace-id']) { |
| 19 | + headers['x-trace-id'] = req.headers['x-trace-id']; |
| 20 | + } |
| 21 | + if (req.headers['x-rpc-id']) { |
| 22 | + headers['x-rpc-id'] = req.headers['x-rpc-id']; |
| 23 | + } |
| 24 | + |
| 25 | + res.writeHead(200, headers); |
| 26 | + res.end(JSON.stringify({ ok: true })); |
| 27 | + }); |
| 28 | + |
| 29 | + await new Promise(resolve => { |
| 30 | + mockServer.listen(0, '127.0.0.1', resolve); |
| 31 | + }); |
| 32 | + |
| 33 | + app = utils.app('apps/fetch-tracer'); |
| 34 | + await app.ready(); |
| 35 | + }); |
| 36 | + |
| 37 | + after(() => { |
| 38 | + if (mockServer?.listening) { |
| 39 | + mockServer.close(); |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + it('should add tracer headers when fetch is called', async () => { |
| 44 | + const port = mockServer.address().port; |
| 45 | + const targetUrl = `http://127.0.0.1:${port}/mock`; |
| 46 | + |
| 47 | + const response = await app.httpRequest() |
| 48 | + .get('/test') |
| 49 | + .query({ url: targetUrl }) |
| 50 | + .expect(200); |
| 51 | + |
| 52 | + assert.strictEqual(response.body.status, 200); |
| 53 | + assert.strictEqual(response.body.ok, true); |
| 54 | + |
| 55 | + // Verify tracer headers were added with incremented rpcId |
| 56 | + assert.strictEqual(response.headers['x-trace-id'], 'test-trace-id-123'); |
| 57 | + assert.strictEqual(response.headers['x-rpc-id'], '0.1'); // rpcIdPlus increments from 0 |
| 58 | + }); |
| 59 | + |
| 60 | + it('should work when tracer is not set', async () => { |
| 61 | + // Clear currentContext |
| 62 | + app.currentContext = null; |
| 63 | + |
| 64 | + const port = mockServer.address().port; |
| 65 | + const targetUrl = `http://127.0.0.1:${port}/mock`; |
| 66 | + |
| 67 | + const response = await app.fetch(targetUrl); |
| 68 | + |
| 69 | + assert.strictEqual(response.status, 200); |
| 70 | + |
| 71 | + // Verify no tracer headers when tracer is not set |
| 72 | + assert.strictEqual(response.headers.get('x-trace-id'), null); |
| 73 | + assert.strictEqual(response.headers.get('x-rpc-id'), null); |
| 74 | + }); |
| 75 | + |
| 76 | + |
| 77 | + it('should handle fetch before configDidLoad completes', async () => { |
| 78 | + // Test that lazy initialization preserves interceptors set in configDidLoad |
| 79 | + const port = mockServer.address().port; |
| 80 | + const targetUrl = `http://127.0.0.1:${port}/mock`; |
| 81 | + |
| 82 | + const ctx = app.mockContext(); |
| 83 | + ctx.tracer = new app.Tracer('early-trace-id', '0.1'); |
| 84 | + app.currentContext = ctx; |
| 85 | + |
| 86 | + const response = await app.fetch(targetUrl); |
| 87 | + assert.strictEqual(response.status, 200); |
| 88 | + assert.strictEqual(response.headers.get('x-trace-id'), 'early-trace-id'); |
| 89 | + assert.strictEqual(response.headers.get('x-rpc-id'), '0.1.1'); // rpcIdPlus increments from 0.1 |
| 90 | + }); |
| 91 | + |
| 92 | + it('should increment rpcId on multiple fetch calls', async () => { |
| 93 | + // Test that rpcId increments properly on each fetch |
| 94 | + const port = mockServer.address().port; |
| 95 | + const targetUrl = `http://127.0.0.1:${port}/mock`; |
| 96 | + |
| 97 | + const ctx = app.mockContext(); |
| 98 | + ctx.tracer = new app.Tracer('multi-trace-id', '0'); |
| 99 | + app.currentContext = ctx; |
| 100 | + |
| 101 | + // First fetch |
| 102 | + let response = await app.fetch(targetUrl); |
| 103 | + assert.strictEqual(response.headers.get('x-trace-id'), 'multi-trace-id'); |
| 104 | + assert.strictEqual(response.headers.get('x-rpc-id'), '0.1'); |
| 105 | + |
| 106 | + // Second fetch |
| 107 | + response = await app.fetch(targetUrl); |
| 108 | + assert.strictEqual(response.headers.get('x-trace-id'), 'multi-trace-id'); |
| 109 | + assert.strictEqual(response.headers.get('x-rpc-id'), '0.2'); |
| 110 | + |
| 111 | + // Third fetch |
| 112 | + response = await app.fetch(targetUrl); |
| 113 | + assert.strictEqual(response.headers.get('x-trace-id'), 'multi-trace-id'); |
| 114 | + assert.strictEqual(response.headers.get('x-rpc-id'), '0.3'); |
| 115 | + }); |
| 116 | +}); |
0 commit comments