Skip to content

Commit ad41981

Browse files
issmirnovclaude
andcommitted
test: add tests for custom headers parsing and injection
- config_test.go: 6 test cases covering empty, single, multiple, whitespace-trimmed, colon-in-value, and malformed header pairs - client_test.go: 3 test cases verifying custom headers are sent on requests, and that nil/empty header maps are handled safely - Add doc comments to NewClientWithHeaders, setHeaders, and CustomHeaders field Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c29ce3e commit ad41981

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

internal/client/client_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,3 +1258,65 @@ func TestDoRequest_RetryOn429(t *testing.T) {
12581258
assert.Equal(t, 2, attempts)
12591259
assert.Contains(t, string(result), "success")
12601260
}
1261+
1262+
func TestNewClientWithHeaders_SetsCustomHeaders(t *testing.T) {
1263+
customHeaders := map[string]string{
1264+
"CF-Access-Client-Id": "test-id.access",
1265+
"CF-Access-Client-Secret": "test-secret",
1266+
}
1267+
1268+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1269+
// Verify standard headers are present
1270+
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
1271+
assert.Equal(t, "test-api-key", r.Header.Get("SIGNOZ-API-KEY"))
1272+
1273+
// Verify custom headers are injected
1274+
assert.Equal(t, "test-id.access", r.Header.Get("CF-Access-Client-Id"))
1275+
assert.Equal(t, "test-secret", r.Header.Get("CF-Access-Client-Secret"))
1276+
1277+
w.WriteHeader(http.StatusOK)
1278+
_, _ = w.Write([]byte(`{"status":"success","data":[]}`))
1279+
}))
1280+
defer server.Close()
1281+
1282+
logger, _ := zap.NewDevelopment()
1283+
client := NewClientWithHeaders(logger, server.URL, "test-api-key", "SIGNOZ-API-KEY", customHeaders)
1284+
1285+
_, err := client.ListAlerts(context.Background(), types.ListAlertsParams{})
1286+
assert.NoError(t, err)
1287+
}
1288+
1289+
func TestNewClientWithHeaders_NilHeaders(t *testing.T) {
1290+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1291+
// Standard headers should still be set when custom headers map is nil
1292+
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
1293+
assert.Equal(t, "test-api-key", r.Header.Get("SIGNOZ-API-KEY"))
1294+
1295+
w.WriteHeader(http.StatusOK)
1296+
_, _ = w.Write([]byte(`{"status":"success","data":[]}`))
1297+
}))
1298+
defer server.Close()
1299+
1300+
logger, _ := zap.NewDevelopment()
1301+
client := NewClientWithHeaders(logger, server.URL, "test-api-key", "SIGNOZ-API-KEY", nil)
1302+
1303+
_, err := client.ListAlerts(context.Background(), types.ListAlertsParams{})
1304+
assert.NoError(t, err)
1305+
}
1306+
1307+
func TestNewClientWithHeaders_EmptyHeaders(t *testing.T) {
1308+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1309+
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
1310+
assert.Equal(t, "test-api-key", r.Header.Get("SIGNOZ-API-KEY"))
1311+
1312+
w.WriteHeader(http.StatusOK)
1313+
_, _ = w.Write([]byte(`{"status":"success","data":[]}`))
1314+
}))
1315+
defer server.Close()
1316+
1317+
logger, _ := zap.NewDevelopment()
1318+
client := NewClientWithHeaders(logger, server.URL, "test-api-key", "SIGNOZ-API-KEY", map[string]string{})
1319+
1320+
_, err := client.ListAlerts(context.Background(), types.ListAlertsParams{})
1321+
assert.NoError(t, err)
1322+
}

internal/config/config_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestLoadConfig_CustomHeaders(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
envValue string
15+
expectedHeaders map[string]string
16+
}{
17+
{
18+
name: "empty env var produces empty map",
19+
envValue: "",
20+
expectedHeaders: map[string]string{},
21+
},
22+
{
23+
name: "single header pair",
24+
envValue: "X-Custom-Auth:my-token",
25+
expectedHeaders: map[string]string{
26+
"X-Custom-Auth": "my-token",
27+
},
28+
},
29+
{
30+
name: "multiple header pairs",
31+
envValue: "CF-Access-Client-Id:abc123.access,CF-Access-Client-Secret:secret456",
32+
expectedHeaders: map[string]string{
33+
"CF-Access-Client-Id": "abc123.access",
34+
"CF-Access-Client-Secret": "secret456",
35+
},
36+
},
37+
{
38+
name: "whitespace is trimmed",
39+
envValue: " Key1 : Value1 , Key2 : Value2 ",
40+
expectedHeaders: map[string]string{
41+
"Key1": "Value1",
42+
"Key2": "Value2",
43+
},
44+
},
45+
{
46+
name: "value containing colon is preserved",
47+
envValue: "Authorization:Bearer my-jwt-token:with:colons",
48+
expectedHeaders: map[string]string{
49+
"Authorization": "Bearer my-jwt-token:with:colons",
50+
},
51+
},
52+
{
53+
name: "malformed entry without colon is skipped",
54+
envValue: "ValidKey:ValidValue,MalformedEntry",
55+
expectedHeaders: map[string]string{
56+
"ValidKey": "ValidValue",
57+
},
58+
},
59+
}
60+
61+
for _, tt := range tests {
62+
t.Run(tt.name, func(t *testing.T) {
63+
// Set required env vars so LoadConfig doesn't fail
64+
os.Setenv("SIGNOZ_URL", "http://localhost:8080")
65+
os.Setenv("SIGNOZ_API_KEY", "test-key")
66+
defer os.Unsetenv("SIGNOZ_URL")
67+
defer os.Unsetenv("SIGNOZ_API_KEY")
68+
69+
if tt.envValue != "" {
70+
os.Setenv("SIGNOZ_CUSTOM_HEADERS", tt.envValue)
71+
defer os.Unsetenv("SIGNOZ_CUSTOM_HEADERS")
72+
} else {
73+
os.Unsetenv("SIGNOZ_CUSTOM_HEADERS")
74+
}
75+
76+
cfg, err := LoadConfig()
77+
require.NoError(t, err)
78+
assert.Equal(t, tt.expectedHeaders, cfg.CustomHeaders)
79+
})
80+
}
81+
}

0 commit comments

Comments
 (0)