Skip to content

Commit 81e8b56

Browse files
committed
Add table tests for influx lib
1 parent 842eaf5 commit 81e8b56

File tree

1 file changed

+64
-31
lines changed

1 file changed

+64
-31
lines changed

test/influx.test.js

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,75 @@
1-
import { describe, it, expect, vi } from 'vitest';
1+
import { describe, it, expect, vi, beforeEach } from 'vitest';
22
import { reportRequestMetric } from '../lib/influx.js';
33

44
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+
};
1412

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(() => {
2314
vi.useFakeTimers();
2415
vi.setSystemTime(date);
25-
2616
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+
];
2757

28-
await reportRequestMetric(request, env);
58+
testCases.forEach(({ description, request, expectedApiKey }) => {
59+
it(description, async () => {
60+
await reportRequestMetric(request, env);
2961

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+
});
4174
});
4275
});

0 commit comments

Comments
 (0)