forked from bots-garden/ori
| ✨ Workspace panel, selectors, previews, desktop app, sandbox template, resizable file tree, light/dark theme | 1 | package health |
| 2 | ||
| 3 | import ( | |
| 4 | "context" | |
| 5 | "net/http" | |
| 6 | "net/http/httptest" | |
| 7 | "strings" | |
| 8 | "testing" | |
| 9 | ) | |
| 10 | ||
| 11 | func 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 | ||
| 34 | func 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 | ||
| 47 | func 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 | ||
| 61 | func 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 | ||
| 73 | func 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 | } |