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