summaryrefslogtreecommitdiff
path: root/internal/daemon/daemon_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/daemon/daemon_test.go')
-rw-r--r--internal/daemon/daemon_test.go81
1 files changed, 81 insertions, 0 deletions
diff --git a/internal/daemon/daemon_test.go b/internal/daemon/daemon_test.go
index a3ff0a6..44f9888 100644
--- a/internal/daemon/daemon_test.go
+++ b/internal/daemon/daemon_test.go
@@ -3,6 +3,7 @@ package daemon
import (
"bytes"
"context"
+ "fmt"
"io"
"log/slog"
"net/http"
@@ -101,6 +102,86 @@ func TestLivez(t *testing.T) {
}
}
+func TestRootHTML(t *testing.T) {
+ fixtures := filepath.Join("..", "..", "fixtures")
+ srv := httptest.NewServer(testHandler(t, fixtures))
+ defer srv.Close()
+ res, err := http.Get(srv.URL + "/")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer res.Body.Close()
+ if res.StatusCode != http.StatusOK {
+ t.Fatalf("status %d", res.StatusCode)
+ }
+ ct := res.Header.Get("Content-Type")
+ if !strings.HasPrefix(ct, "text/html") {
+ t.Fatalf("content type %q", ct)
+ }
+ body, _ := io.ReadAll(res.Body)
+ if !strings.Contains(string(body), "<!DOCTYPE html>") {
+ t.Fatalf("expected html body, got %q", string(body))
+ }
+}
+
+func TestRootCacheReuseAndExpiry(t *testing.T) {
+ cache := &htmlCache{}
+ nowTime := time.Unix(1000, 0)
+ now := func() time.Time { return nowTime }
+ renderCalls := 0
+ h := rootWithCachedRenderer(cache, now, 10*time.Minute, func(context.Context) ([]byte, error) {
+ renderCalls++
+ return []byte(fmt.Sprintf("<html>%d</html>", renderCalls)), nil
+ })
+
+ req := httptest.NewRequest(http.MethodGet, "http://example/", nil)
+ w := httptest.NewRecorder()
+ h(w, req)
+ if w.Code != http.StatusOK {
+ t.Fatalf("status %d", w.Code)
+ }
+ first := w.Body.String()
+ if renderCalls != 1 {
+ t.Fatalf("render calls %d want 1", renderCalls)
+ }
+
+ w2 := httptest.NewRecorder()
+ h(w2, req)
+ if w2.Code != http.StatusOK {
+ t.Fatalf("status %d", w2.Code)
+ }
+ if w2.Body.String() != first {
+ t.Fatalf("cached body mismatch: %q != %q", w2.Body.String(), first)
+ }
+ if renderCalls != 1 {
+ t.Fatalf("render calls %d want 1", renderCalls)
+ }
+
+ nowTime = nowTime.Add(11 * time.Minute)
+ w3 := httptest.NewRecorder()
+ h(w3, req)
+ if w3.Code != http.StatusOK {
+ t.Fatalf("status %d", w3.Code)
+ }
+ if renderCalls != 2 {
+ t.Fatalf("render calls %d want 2", renderCalls)
+ }
+}
+
+func TestRootNotFoundForUnknownPath(t *testing.T) {
+ fixtures := filepath.Join("..", "..", "fixtures")
+ srv := httptest.NewServer(testHandler(t, fixtures))
+ defer srv.Close()
+ res, err := http.Get(srv.URL + "/no-such-route")
+ if err != nil {
+ t.Fatal(err)
+ }
+ res.Body.Close()
+ if res.StatusCode != http.StatusNotFound {
+ t.Fatalf("status %d", res.StatusCode)
+ }
+}
+
func TestReadyzOK(t *testing.T) {
srv := httptest.NewServer(testHandler(t, t.TempDir()))
defer srv.Close()