server_test.go (1687B)
1 package main 2 3 import ( 4 "hivedav/config" 5 "net/http" 6 "net/http/httptest" 7 "strings" 8 "testing" 9 ) 10 11 var server *Server 12 13 func TestServer(t *testing.T) { 14 config := config.Config{ 15 ListenAddress: "[::]", 16 ListenPort: 3737, 17 CaldavUri: "/", 18 Calendar: 0, 19 CaldavUser: "gandalf", 20 CaldavPassword: "ring", 21 } 22 server, err := server.NewServer(&config) 23 if err != nil { 24 t.Fatal("Could not create hivedav server") 25 } 26 27 // ResponseRecorder is an implementation of http.ResponseWriter that 28 // records its mutations for later inspection in tests. 29 // https://pkg.go.dev/net/http/httptest 30 rr := httptest.NewRecorder() 31 32 request, err := http.NewRequest("GET", "/", nil) 33 34 // test curlable UI 35 request.Header.Set("user-agent", "curl/8.1.2") 36 server.Index(rr, request, nil) 37 38 if err != nil { 39 t.Fatal("error creating new request:", err) 40 } 41 42 if status := rr.Code; status != http.StatusOK { 43 t.Errorf("request to / returned wrong response code. Got %v, want %v", 44 status, http.StatusOK) 45 } 46 47 welcome := rr.Body.String() 48 if !strings.Contains(welcome, "Hive") { 49 t.Errorf("request to / does not contain word 'Hive'") 50 } 51 52 // test regular browser UI 53 request.Header.Set("user-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36") 54 server.Index(rr, request, nil) 55 56 if err != nil { 57 t.Fatal("error creating new request:", err) 58 } 59 60 if status := rr.Code; status != http.StatusOK { 61 t.Errorf("request to / returned wrong response code. Got %v, want %v", 62 status, http.StatusOK) 63 } 64 65 welcome = rr.Body.String() 66 if !strings.Contains(welcome, "hive") { 67 t.Errorf("request to / does not contain word 'hive'") 68 } 69 }