|
1 | 1 | import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' |
2 | | -import { createMetricsFromRequest, writeMetrics } from '../lib/influx.js' |
| 2 | +import { createMetricsFromRequest, writeMetrics, reportRequestMetric } from '../lib/influx.js' |
| 3 | + |
| 4 | +describe('reportRequestMetric', () => { |
| 5 | + const date = new Date() |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + vi.useFakeTimers() |
| 9 | + vi.setSystemTime(date) |
| 10 | + }) |
| 11 | + |
| 12 | + afterEach(() => { |
| 13 | + vi.useRealTimers() |
| 14 | + }) |
| 15 | + |
| 16 | + it('reports request metrics to InfluxDB over HTTP', async () => { |
| 17 | + const env = withTestEnvitronment() |
| 18 | + const request = { |
| 19 | + url: 'https://example.com/path', |
| 20 | + method: 'GET', |
| 21 | + headers: new Map([['api-key', 'test-key']]), |
| 22 | + } |
| 23 | + global.fetch = vi.fn().mockResolvedValue(new Response(null, { status: 204 })); |
| 24 | + |
| 25 | + await reportRequestMetric(request, env); |
| 26 | + |
| 27 | + expect(global.fetch).toHaveBeenCalledWith( |
| 28 | + 'https://influx.example.com/api/v2/write?&bucket=test_db&precision=ms', |
| 29 | + expect.objectContaining({ |
| 30 | + method: 'POST', |
| 31 | + headers: expect.objectContaining({ |
| 32 | + Authorization: 'Token test_token', |
| 33 | + 'Content-Type': 'application/octet-stream', |
| 34 | + }), |
| 35 | + body: `test_metric api_key="test-key" ${date.getTime()}`, |
| 36 | + }), |
| 37 | + ) |
| 38 | + }); |
| 39 | +}); |
3 | 40 |
|
4 | 41 | describe('createMetricsFromRequest', () => { |
5 | 42 | const date = new Date() |
@@ -65,9 +102,7 @@ describe('writeMetrics', () => { |
65 | 102 | it('send request metrics to InfluxDB over HTTP', async () => { |
66 | 103 | const env = withTestEnvitronment() |
67 | 104 | const lineProtocolData = 'test_metric api_key="test-key"' |
68 | | - global.fetch = vi |
69 | | - .fn() |
70 | | - .mockResolvedValue(new Response(null, { status: 204 })) |
| 105 | + global.fetch = vi.fn().mockResolvedValue(new Response(null, { status: 204 })) |
71 | 106 |
|
72 | 107 | const response = await writeMetrics(lineProtocolData, env) |
73 | 108 |
|
|
0 commit comments