From 1326bb4103694d7ceac23b23329997ea2207a3f6 Mon Sep 17 00:00:00 2001 From: Marc Pervaz Boocha Date: Thu, 7 Aug 2025 22:51:34 +0530 Subject: Added File Mode to sockets and socket activation --- http/http_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 http/http_test.go (limited to 'http/http_test.go') diff --git a/http/http_test.go b/http/http_test.go new file mode 100644 index 0000000..603dce3 --- /dev/null +++ b/http/http_test.go @@ -0,0 +1,54 @@ +package http_test + +import ( + "context" + httpServer "go.sudomsg.com/kit/http" + "io" + "net/http" + "strings" + "testing" + "time" +) + +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) + } + }) +} + -- cgit v1.2.3-70-g09d2