|
1 | | -import { describe, it, expect, vi } from 'vitest'; |
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
2 | 2 | import { reportRequestMetric } from '../lib/influx.js'; |
3 | 3 |
|
4 | 4 | describe('reportRequestMetric', () => { |
5 | | - it('should report metrics to InfluxDB over HTTP', async () => { |
6 | | - const request = { |
7 | | - url: 'https://example.com/path?api-key=test-key', |
8 | | - method: 'GET', |
9 | | - headers: new Map([ |
10 | | - ['api-key', 'test-key'], |
11 | | - ['Authorization', 'Bearer test-token'] |
12 | | - ]) |
13 | | - }; |
| 5 | + const date = new Date(); |
| 6 | + const env = { |
| 7 | + INFLUX_METRIC_NAME: 'test_metric', |
| 8 | + INFLUX_URL: 'https://influx.example.com', |
| 9 | + INFLUX_DATABASE: 'test_db', |
| 10 | + INFLUX_TOKEN: 'test_token' |
| 11 | + }; |
14 | 12 |
|
15 | | - const env = { |
16 | | - INFLUX_METRIC_NAME: 'test_metric', |
17 | | - INFLUX_URL: 'https://influx.example.com', |
18 | | - INFLUX_DATABASE: 'test_db', |
19 | | - INFLUX_TOKEN: 'test_token' |
20 | | - }; |
21 | | - |
22 | | - const date = new Date(); |
| 13 | + beforeEach(() => { |
23 | 14 | vi.useFakeTimers(); |
24 | 15 | vi.setSystemTime(date); |
25 | | - |
26 | 16 | global.fetch = vi.fn().mockResolvedValue({ status: 204 }); |
| 17 | + }); |
| 18 | + |
| 19 | + const testCases = [ |
| 20 | + { |
| 21 | + description: 'when request has an API key in the Authorization header', |
| 22 | + request: { |
| 23 | + url: 'https://example.com/path', |
| 24 | + method: 'GET', |
| 25 | + headers: new Map([['Authorization', 'Bearer test-key']]) |
| 26 | + }, |
| 27 | + expectedApiKey: 'test-key' |
| 28 | + }, |
| 29 | + { |
| 30 | + description: 'when request has an API key in the api-key header', |
| 31 | + request: { |
| 32 | + url: 'https://example.com/path', |
| 33 | + method: 'GET', |
| 34 | + headers: new Map([['api-key', 'test-key']]) |
| 35 | + }, |
| 36 | + expectedApiKey: 'test-key' |
| 37 | + }, |
| 38 | + { |
| 39 | + description: 'when request has an API key in the query string', |
| 40 | + request: { |
| 41 | + url: 'https://example.com/path?api-key=test-key', |
| 42 | + method: 'GET', |
| 43 | + headers: new Map() |
| 44 | + }, |
| 45 | + expectedApiKey: 'test-key' |
| 46 | + }, |
| 47 | + { |
| 48 | + description: 'when request has no API key', |
| 49 | + request: { |
| 50 | + url: 'https://example.com/path', |
| 51 | + method: 'GET', |
| 52 | + headers: new Map() |
| 53 | + }, |
| 54 | + expectedApiKey: 'unknown' |
| 55 | + } |
| 56 | + ]; |
27 | 57 |
|
28 | | - await reportRequestMetric(request, env); |
| 58 | + testCases.forEach(({ description, request, expectedApiKey }) => { |
| 59 | + it(description, async () => { |
| 60 | + await reportRequestMetric(request, env); |
29 | 61 |
|
30 | | - expect(global.fetch).toHaveBeenCalledWith( |
31 | | - 'https://influx.example.com/api/v2/write?&bucket=test_db&precision=ms', |
32 | | - expect.objectContaining({ |
33 | | - method: 'POST', |
34 | | - headers: expect.objectContaining({ |
35 | | - 'Authorization': 'Token test_token', |
36 | | - 'Content-Type': 'application/octet-stream' |
37 | | - }), |
38 | | - body: `test_metric api_key="test-key" ${date.valueOf()}` |
39 | | - }) |
40 | | - ); |
| 62 | + expect(global.fetch).toHaveBeenCalledWith( |
| 63 | + 'https://influx.example.com/api/v2/write?&bucket=test_db&precision=ms', |
| 64 | + expect.objectContaining({ |
| 65 | + method: 'POST', |
| 66 | + headers: expect.objectContaining({ |
| 67 | + 'Authorization': 'Token test_token', |
| 68 | + 'Content-Type': 'application/octet-stream' |
| 69 | + }), |
| 70 | + body: `test_metric api_key="${expectedApiKey}" ${date.valueOf()}` |
| 71 | + }) |
| 72 | + ); |
| 73 | + }); |
41 | 74 | }); |
42 | 75 | }); |
0 commit comments