fix: fix test

This commit is contained in:
Jonas Kaninda
2024-11-12 15:36:59 +01:00
parent 5bc85c9814
commit 2ee6d89fb4

View File

@@ -3,7 +3,6 @@ package pkg
import ( import (
"context" "context"
"log" "log"
"net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
"path/filepath" "path/filepath"
@@ -23,32 +22,31 @@ func TestInit(t *testing.T) {
func TestCheckConfig(t *testing.T) { func TestCheckConfig(t *testing.T) {
TestInit(t) TestInit(t)
initConfig(configFile) err := initConfig(configFile)
err := CheckConfig(configFile)
if err != nil { if err != nil {
t.Error(err) t.Fatalf(err.Error())
}
err = CheckConfig(configFile)
if err != nil {
t.Fatalf(err.Error())
} }
log.Println("Goma Gateway configuration file checked successfully") log.Println("Goma Gateway configuration file checked successfully")
} }
func TestStart(t *testing.T) { func TestStart(t *testing.T) {
TestInit(t) TestInit(t)
initConfig(configFile) err := initConfig(configFile)
if err != nil {
t.Fatalf(err.Error())
}
g := GatewayServer{} g := GatewayServer{}
gatewayServer, err := g.Config(configFile) gatewayServer, err := g.Config(configFile)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
route := gatewayServer.Initialize() route := gatewayServer.Initialize()
assertResponseBody := func(t *testing.T, s *httptest.Server) {
route.HandleFunc("/test", func(rw http.ResponseWriter, r *http.Request) { resp, err := s.Client().Get(s.URL + "/health/live")
_, err := rw.Write([]byte("Hello Goma Proxy"))
if err != nil {
t.Fatalf("Failed writing HTTP response: %v", err)
}
})
assertResponseBody := func(t *testing.T, s *httptest.Server, expectedBody string) {
resp, err := s.Client().Get(s.URL)
if err != nil { if err != nil {
t.Fatalf("unexpected error getting from server: %v", err) t.Fatalf("unexpected error getting from server: %v", err)
} }
@@ -68,7 +66,7 @@ func TestStart(t *testing.T) {
t.Run("httpServer", func(t *testing.T) { t.Run("httpServer", func(t *testing.T) {
s := httptest.NewServer(route) s := httptest.NewServer(route)
defer s.Close() defer s.Close()
assertResponseBody(t, s, "Hello Goma Proxy") assertResponseBody(t, s)
}) })
ctx.Done() ctx.Done()
} }