Files
x/internal/plugin/plugin_test.go
T
ginuerzh 0e96f602fa fix(matcher,plugin): case-insensitive matching, port accumulation, nil guards, doc comments
matcher: fix IPv6 normalization via netip.ParseAddr, port-range overwrite
by switching to []*PortRange map, case-insensitive domain/host matching,
glob.MustCompile→Compile to avoid panic on invalid patterns.

plugin: guard nil *Options in NewGRPCConn and NewHTTPClient; add
HTTPClientTransport helper for idle-connection cleanup.

net/*: add doc comments for all exported symbols (no code changes).

Add 30 tests (matcher_test.go 22, plugin_test.go 8) covering all
matcher types, nil safety, case insensitivity, IPv6 normalization,
port ranges, and plugin option composition.
2026-05-24 18:18:42 +08:00

113 lines
2.4 KiB
Go

package plugin
import (
"net/http"
"testing"
"time"
)
func TestNewGRPCConn_NilOpts(t *testing.T) {
conn, err := NewGRPCConn("localhost:0", nil)
if err != nil {
t.Fatal(err)
}
if conn == nil {
t.Fatal("expected non-nil conn")
}
conn.Close()
}
func TestNewGRPCConn_NoTLS(t *testing.T) {
conn, err := NewGRPCConn("localhost:0", &Options{})
if err != nil {
t.Fatal(err)
}
if conn == nil {
t.Fatal("expected non-nil conn")
}
conn.Close()
}
func TestNewGRPCConn_WithTLS(t *testing.T) {
conn, err := NewGRPCConn("localhost:0", &Options{
TLSConfig: nil, // nil TLSConfig -> insecure
})
if err != nil {
t.Fatal(err)
}
if conn == nil {
t.Fatal("expected non-nil conn")
}
conn.Close()
}
func TestNewHTTPClient_NilOpts(t *testing.T) {
c := NewHTTPClient(nil)
if c == nil {
t.Fatal("expected non-nil client")
}
}
func TestNewHTTPClient_Defaults(t *testing.T) {
c := NewHTTPClient(&Options{})
if c == nil {
t.Fatal("expected non-nil client")
}
if c.Timeout != 0 {
t.Errorf("expected zero timeout, got %v", c.Timeout)
}
}
func TestNewHTTPClient_WithTimeout(t *testing.T) {
c := NewHTTPClient(&Options{Timeout: 5 * time.Second})
if c.Timeout != 5*time.Second {
t.Errorf("expected 5s timeout, got %v", c.Timeout)
}
}
func TestHTTPClientTransport_Nil(t *testing.T) {
if tr := HTTPClientTransport(nil); tr != nil {
t.Error("expected nil transport for nil client")
}
}
func TestHTTPClientTransport_Valid(t *testing.T) {
c := NewHTTPClient(&Options{})
tr := HTTPClientTransport(c)
if tr == nil {
t.Fatal("expected non-nil transport")
}
// Verify we can call CloseIdleConnections without panic.
tr.CloseIdleConnections()
}
func TestOption(t *testing.T) {
opts := &Options{}
TokenOption("tok")(opts)
TLSConfigOption(nil)(opts)
HeaderOption(http.Header{"X": []string{"y"}})(opts)
TimeoutOption(time.Second)(opts)
if opts.Token != "tok" {
t.Errorf("expected Token 'tok', got %q", opts.Token)
}
if opts.Timeout != time.Second {
t.Errorf("expected 1s timeout, got %v", opts.Timeout)
}
if opts.Header.Get("X") != "y" {
t.Errorf("expected header X=y, got %v", opts.Header)
}
}
func TestNewHTTPClient_TransportTLSConfig(t *testing.T) {
// nil TLSConfig — transport should still exist.
c := NewHTTPClient(&Options{})
tr := HTTPClientTransport(c)
if tr == nil {
t.Fatal("expected non-nil transport")
}
if tr.TLSClientConfig != nil {
t.Error("expected nil TLSClientConfig")
}
}