aboutsummaryrefslogtreecommitdiffstats
path: root/http/server_test.go
diff options
context:
space:
mode:
authorMarc Pervaz Boocha <mboocha@sudomsg.com>2025-08-07 22:51:34 +0530
committerMarc Pervaz Boocha <mboocha@sudomsg.com>2025-08-07 22:51:34 +0530
commit1326bb4103694d7ceac23b23329997ea2207a3f6 (patch)
tree72eb0065b597121c4e54518d303f5d15de40336d /http/server_test.go
parentFixed missing signals (diff)
downloadkit-1326bb4103694d7ceac23b23329997ea2207a3f6.tar
kit-1326bb4103694d7ceac23b23329997ea2207a3f6.tar.gz
kit-1326bb4103694d7ceac23b23329997ea2207a3f6.tar.bz2
kit-1326bb4103694d7ceac23b23329997ea2207a3f6.tar.lz
kit-1326bb4103694d7ceac23b23329997ea2207a3f6.tar.xz
kit-1326bb4103694d7ceac23b23329997ea2207a3f6.tar.zst
kit-1326bb4103694d7ceac23b23329997ea2207a3f6.zip
Added File Mode to sockets and socket activation
Diffstat (limited to '')
-rw-r--r--http/server_test.go141
1 files changed, 81 insertions, 60 deletions
diff --git a/http/server_test.go b/http/server_test.go
index 7138c73..0f49f01 100644
--- a/http/server_test.go
+++ b/http/server_test.go
@@ -1,15 +1,11 @@
package http_test
import (
- "context"
- "io"
"net"
- "net/http"
"strings"
"testing"
- "time"
- httpServer "go.sudomsg.com/kit/http"
+ "go.sudomsg.com/kit/http"
)
// helper to find free TCP ports
@@ -29,12 +25,12 @@ func TestOpenConfigListeners(t *testing.T) {
t.Parallel()
ctx := t.Context()
- cfg := []httpServer.ServerConfig{
- {Network: "tcp", Address: "localhost:0"},
- {Network: "tcp", Address: "localhost:0"},
+ cfg := []http.ServerConfig{
+ {Network: http.NetTCP, Address: "localhost:0"},
+ {Network: http.NetTCP, Address: "localhost:0"},
}
- lns, err := httpServer.OpenConfigListners(ctx, cfg)
+ lns, err := http.OpenConfigListners(ctx, cfg)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
@@ -52,12 +48,12 @@ func TestOpenConfigListeners(t *testing.T) {
conflict := newListener(t)
defer conflict.Close()
- cfg := []httpServer.ServerConfig{
- {Network: "tcp", Address: "localhost:0"},
- {Network: "tcp", Address: conflict.Addr().String()}, // will fail
+ cfg := []http.ServerConfig{
+ {Network: http.NetTCP, Address: "localhost:0"},
+ {Network: http.NetTCP, Address: conflict.Addr().String()}, // will fail
}
- lns, err := httpServer.OpenConfigListners(ctx, cfg)
+ lns, err := http.OpenConfigListners(ctx, cfg)
if err == nil {
defer lns.CloseAll()
t.Fatal("expected error due to conflict, got nil")
@@ -71,7 +67,7 @@ func TestCloseAll(t *testing.T) {
ln1 := newListener(t)
ln2 := newListener(t)
- ls := httpServer.Listeners{ln1, ln2}
+ ls := http.Listeners{ln1, ln2}
err := ls.CloseAll()
if err != nil {
t.Errorf("unexpected error from CloseAll: %v", err)
@@ -85,62 +81,20 @@ func TestCloseAll(t *testing.T) {
})
}
-func TestRunHTTPServers(t *testing.T) {
- t.Parallel()
- t.Run("basic serve and shutdown", func(t *testing.T) {
- t.Parallel()
- ctx := t.Context()
- handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- io.WriteString(w, "hello")
- })
-
- ln := newListener(t)
- addr := ln.Addr().String()
-
- ctx, cancel := context.WithCancel(ctx)
- defer cancel()
-
- go func() {
- // Wait for server to be ready
- time.Sleep(200 * time.Millisecond)
-
- r, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://"+addr, nil)
- resp, err := http.DefaultClient.Do(r)
- if err != nil {
- t.Errorf("http.Do error: %v", err)
- return
- }
- defer resp.Body.Close()
-
- body, _ := io.ReadAll(resp.Body)
- if got := strings.TrimSpace(string(body)); got != "hello" {
- t.Errorf("unexpected response body: %q", got)
- }
-
- cancel() // shutdown the server
- }()
-
- err := httpServer.RunHTTPServers(ctx, httpServer.Listeners{ln}, handler)
- if err != nil {
- t.Fatalf("RunHTTPServers failed: %v", err)
- }
- })
-}
-
func TestInvalidConfig(t *testing.T) {
t.Parallel()
tests := []struct {
name string
- config httpServer.ServerConfig
+ config http.ServerConfig
}{
{
name: "invalid network",
- config: httpServer.ServerConfig{Network: "invalid", Address: "localhost:0"},
+ config: http.ServerConfig{Network: -1, Address: "localhost:0"},
},
{
name: "invalid address",
- config: httpServer.ServerConfig{Network: "tcp", Address: "::::"},
+ config: http.ServerConfig{Network: http.NetTCP, Address: "::::"},
},
}
@@ -150,10 +104,77 @@ func TestInvalidConfig(t *testing.T) {
ctx := t.Context()
- _, err := httpServer.OpenConfigListners(ctx, []httpServer.ServerConfig{tt.config})
+ _, err := http.OpenConfigListners(ctx, []http.ServerConfig{tt.config})
if err == nil {
t.Fatal("OpenConfigListners() expected error, got nil")
}
})
}
}
+
+func TestNetType(t *testing.T) {
+ t.Parallel()
+
+ t.Run("RoundTrip", func(t *testing.T) {
+ t.Parallel()
+
+ testCases := []http.NetType{
+ http.NetTCP,
+ http.NetTCP4,
+ http.NetTCP6,
+ http.NetUnix,
+ http.NetUnixPacket,
+ }
+
+ for _, original := range testCases {
+ t.Run(original.String(), func(t *testing.T) {
+ t.Run("case same", func(t *testing.T) {
+ t.Run(original.String(), func(t *testing.T) {
+ text, err := original.MarshalText()
+ if err != nil {
+ t.Fatalf("MarshalText failed: %v", err)
+ }
+
+ var decoded http.NetType
+ if err := decoded.UnmarshalText(text); err != nil {
+ t.Fatalf("UnmarshalText failed: %v", err)
+ }
+
+ if decoded != original {
+ t.Fatalf("Round-trip mismatch: got %v, want %v", decoded, original)
+ }
+ })
+ })
+ t.Run("case not same", func(t *testing.T) {
+ t.Run(original.String(), func(t *testing.T) {
+ text, err := original.MarshalText()
+ if err != nil {
+ t.Fatalf("MarshalText failed: %v", err)
+ }
+
+ var decoded http.NetType
+ if err := decoded.UnmarshalText([]byte(strings.ToUpper(string(text)))); err != nil {
+ t.Fatalf("UnmarshalText failed: %v", err)
+ }
+
+ if decoded != original {
+ t.Fatalf("Round-trip mismatch: got %v, want %v", decoded, original)
+ }
+ })
+ })
+
+ })
+ }
+ })
+
+ t.Run("Invalid", func(t *testing.T) {
+ t.Parallel()
+
+ input := "invalid"
+ var decoded http.NetType
+
+ if err := decoded.UnmarshalText([]byte(input)); err == nil {
+ t.Errorf("expected error for input %q, got none", input)
+ }
+ })
+}