Skip to content

Commit c43b0d5

Browse files
committed
Add test for reportRequestMetric
1 parent 8fc77ed commit c43b0d5

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

test/influx.test.js

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
11
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+
});
340

441
describe('createMetricsFromRequest', () => {
542
const date = new Date()
@@ -65,9 +102,7 @@ describe('writeMetrics', () => {
65102
it('send request metrics to InfluxDB over HTTP', async () => {
66103
const env = withTestEnvitronment()
67104
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 }))
71106

72107
const response = await writeMetrics(lineProtocolData, env)
73108

test/worker.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ describe('worker.fetch', () => {
1111
const ctx = {
1212
waitUntil: vi.fn()
1313
};
14-
global.fetch = vi.fn().mockResolvedValue(new Response(null, { status: 204 }));
14+
global.fetch = vi.fn().mockResolvedValue(new Response(null, { status: 200 }));
1515
const reportRequestMetric = vi.fn()
1616
const response = await worker.fetch(request, env, ctx, { reportRequestMetric });
1717
expect(global.fetch).toHaveBeenCalledWith(request);
18-
expect(ctx.waitUntil).toHaveBeenCalledWith(reportRequestMetric(request, env));
1918
expect(reportRequestMetric).toHaveBeenCalledWith(request, env);
20-
expect(response.status).toBe(204);
19+
expect(ctx.waitUntil).toHaveBeenCalledWith(reportRequestMetric(request, env));
20+
expect(response.status).toBe(200);
2121
});
2222
});

0 commit comments

Comments
 (0)