Files
goma-gateway/internal/server_test.go

73 lines
1.4 KiB
Go
Raw Normal View History

2024-10-27 06:10:27 +01:00
package pkg
import (
"context"
"log"
2024-10-27 06:10:27 +01:00
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
const testPath = "./tests"
var configFile = filepath.Join(testPath, "goma.yml")
func TestInit(t *testing.T) {
err := os.MkdirAll(testPath, os.ModePerm)
if err != nil {
t.Error(err)
}
}
func TestCheckConfig(t *testing.T) {
TestInit(t)
2024-11-12 15:36:59 +01:00
err := initConfig(configFile)
if err != nil {
t.Fatal("Error init config:", err)
2024-11-12 15:36:59 +01:00
}
err = CheckConfig(configFile)
if err != nil {
t.Fatalf("Error checking config: %s", err.Error())
}
log.Println("Goma Gateway configuration file checked successfully")
}
2024-10-27 06:10:27 +01:00
func TestStart(t *testing.T) {
TestInit(t)
2024-11-12 15:36:59 +01:00
err := initConfig(configFile)
if err != nil {
t.Fatalf("Error initializing config: %s", err.Error())
2024-11-12 15:36:59 +01:00
}
2024-10-27 06:10:27 +01:00
g := GatewayServer{}
gatewayServer, err := g.Config(configFile)
if err != nil {
t.Error(err)
}
route := gatewayServer.Initialize()
2024-11-12 15:36:59 +01:00
assertResponseBody := func(t *testing.T, s *httptest.Server) {
resp, err := s.Client().Get(s.URL + "/health/live")
2024-10-27 06:10:27 +01:00
if err != nil {
t.Fatalf("unexpected error getting from server: %v", err)
}
if resp.StatusCode != 200 {
t.Fatalf("expected a status code of 200, got %v", resp.StatusCode)
}
}
ctx := context.Background()
go func() {
err = gatewayServer.Start(ctx)
if err != nil {
t.Error(err)
return
}
}()
2024-10-27 06:10:27 +01:00
t.Run("httpServer", func(t *testing.T) {
s := httptest.NewServer(route)
defer s.Close()
2024-11-12 15:36:59 +01:00
assertResponseBody(t, s)
2024-10-27 06:10:27 +01:00
})
ctx.Done()
2024-10-27 06:10:27 +01:00
}