this repo has no description

Add unit tests

+56
+56
main_test.go
··· 1 + package main 2 + 3 + import ( 4 + "fmt" 5 + "net/http" 6 + "net/http/httptest" 7 + "testing" 8 + ) 9 + 10 + func TestRootHandler(t *testing.T) { 11 + req, err := http.NewRequest("GET", "/", nil) 12 + if err != nil { 13 + t.Fatal(err) 14 + } 15 + 16 + rr := httptest.NewRecorder() 17 + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 18 + fmt.Fprintf(w, "Hello from Tangled! 🌀\n") 19 + fmt.Fprintf(w, "This is a simple Go demo app.\n") 20 + }) 21 + 22 + handler.ServeHTTP(rr, req) 23 + 24 + if status := rr.Code; status != http.StatusOK { 25 + t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) 26 + } 27 + 28 + expected := "Hello from Tangled! 🌀\nThis is a simple Go demo app.\n" 29 + if rr.Body.String() != expected { 30 + t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected) 31 + } 32 + } 33 + 34 + func TestHealthHandler(t *testing.T) { 35 + req, err := http.NewRequest("GET", "/health", nil) 36 + if err != nil { 37 + t.Fatal(err) 38 + } 39 + 40 + rr := httptest.NewRecorder() 41 + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 42 + w.WriteHeader(http.StatusOK) 43 + fmt.Fprintf(w, "OK\n") 44 + }) 45 + 46 + handler.ServeHTTP(rr, req) 47 + 48 + if status := rr.Code; status != http.StatusOK { 49 + t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) 50 + } 51 + 52 + expected := "OK\n" 53 + if rr.Body.String() != expected { 54 + t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected) 55 + } 56 + }