nandi/oripublic Fork 0
f5c963af3c1597c0274ce4a99705dbd92a7d1cba
Commits
Clone
git clone https://git.rickub.com/nandi/ori.git
git clone ssh://git@rickub.com/nandi/ori.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

forked from bots-garden/ori

health_test.go · 80 lines · 2.2 KBGo Blame HistoryRaw
✨ Workspace panel, selectors, previews, desktop app, sandbox template, resizable file tree, light/dark theme 76d62ac k33g yesterday1package health
2
3import (
4 "context"
5 "net/http"
6 "net/http/httptest"
7 "strings"
8 "testing"
9)
10
11func TestCheckHealthyServer(t *testing.T) {
12 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
13 if r.URL.Path != "/healthz" || r.Method != http.MethodGet {
14 http.NotFound(w, r)
15 return
16 }
17 w.Header().Set("Content-Type", "application/json")
18 _, _ = w.Write([]byte(`{"status":"ok"}`))
19 }))
20 defer srv.Close()
21
22 rep := Check(context.Background(), srv.Client(), srv.URL)
23 if !rep.OK {
24 t.Fatalf("expected OK, got %+v", rep)
25 }
26 if rep.StatusCode != http.StatusOK || rep.URL != srv.URL+"/healthz" {
27 t.Fatalf("unexpected report %+v", rep)
28 }
29 if !strings.Contains(rep.Detail, "status: ok") {
30 t.Fatalf("detail should quote the status, got %q", rep.Detail)
31 }
32}
33
34func TestCheckNonOriServer(t *testing.T) {
35 srv := httptest.NewServer(http.NotFoundHandler())
36 defer srv.Close()
37
38 rep := Check(context.Background(), srv.Client(), srv.URL)
39 if rep.OK {
40 t.Fatalf("expected failure for 404, got %+v", rep)
41 }
42 if rep.StatusCode != http.StatusNotFound || !strings.Contains(rep.Detail, "404") {
43 t.Fatalf("unexpected report %+v", rep)
44 }
45}
46
47func TestCheckUnreachableServer(t *testing.T) {
48 srv := httptest.NewServer(http.NotFoundHandler())
49 addr := srv.URL
50 srv.Close() // nothing listens there any more
51
52 rep := Check(context.Background(), nil, addr)
53 if rep.OK || rep.StatusCode != 0 {
54 t.Fatalf("expected transport failure, got %+v", rep)
55 }
56 if !strings.Contains(rep.Detail, "cannot reach ori") {
57 t.Fatalf("unexpected detail %q", rep.Detail)
58 }
59}
60
61func TestCheckPlainOKBody(t *testing.T) {
62 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
63 _, _ = w.Write([]byte("OK"))
64 }))
65 defer srv.Close()
66
67 rep := Check(context.Background(), srv.Client(), srv.URL)
68 if !rep.OK || rep.Detail != "ori is reachable" {
69 t.Fatalf("a 2xx with a non-JSON body must still count as healthy, got %+v", rep)
70 }
71}
72
73func TestCheckCancelledContext(t *testing.T) {
74 ctx, cancel := context.WithCancel(context.Background())
75 cancel()
76 rep := Check(ctx, nil, "http://127.0.0.1:1")
77 if rep.OK {
78 t.Fatalf("expected failure with a cancelled context, got %+v", rep)
79 }
80}