caddyhttp: call StaticIPRange.Provision directly in tests

TestStaticIPRangeProvision was re-implementing Provision's loop
instead of calling it. The real Provision only touches the Ranges
field, so a zero-value caddy.Context is enough to exercise it.

Signed-off-by: Mohammed Al Sahaf <msaa1990@gmail.com>
add-tests
Mohammed Al Sahaf 2026-08-30 18:38:16 +03:00
parent cc534df68f
commit 9e6e892bd0
No known key found for this signature in database
1 changed files with 7 additions and 17 deletions

View File

@ -3,6 +3,8 @@ package caddyhttp
import (
"net/netip"
"testing"
"github.com/caddyserver/caddy/v2"
)
func TestCIDRExpressionToPrefix(t *testing.T) {
@ -135,25 +137,13 @@ func TestStaticIPRangeProvision(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &StaticIPRange{Ranges: tt.ranges}
// We can't easily create a caddy.Context here without full module setup,
// but Provision only uses the ranges field, so we test the logic directly.
// The Provision method calls CIDRExpressionToPrefix which we test separately.
var parsedCount int
var gotErr bool
for _, r := range s.Ranges {
_, err := CIDRExpressionToPrefix(r)
if err != nil {
gotErr = true
break
}
parsedCount++
}
err := s.Provision(caddy.Context{})
if gotErr != tt.wantErr {
t.Errorf("provision error = %v, wantErr %v", gotErr, tt.wantErr)
if (err != nil) != tt.wantErr {
t.Errorf("Provision() error = %v, wantErr %v", err, tt.wantErr)
}
if !tt.wantErr && parsedCount != tt.wantLen {
t.Errorf("parsed %d ranges, want %d", parsedCount, tt.wantLen)
if !tt.wantErr && len(s.ranges) != tt.wantLen {
t.Errorf("parsed %d ranges, want %d", len(s.ranges), tt.wantLen)
}
})
}