Files
goma-gateway/internal/server_test.go

87 lines
1.8 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"
2024-11-18 10:03:58 +01:00
const extraRoutePath = "./tests/extra"
2024-10-27 06:10:27 +01:00
var configFile = filepath.Join(testPath, "goma.yml")
func TestInit(t *testing.T) {
err := os.MkdirAll(testPath, os.ModePerm)
if err != nil {
t.Error(err)
}
2024-11-18 10:03:58 +01:00
err = os.MkdirAll(extraRoutePath, os.ModePerm)
if err != nil {
t.Error(err)
}
2024-10-27 06:10:27 +01:00
}
func TestCheckConfig(t *testing.T) {
TestInit(t)
2024-11-18 10:03:58 +01:00
err := initConfiguration(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-18 10:03:58 +01:00
err := initConfiguration(configFile)
2024-11-12 15:36:59 +01:00
if err != nil {
t.Fatalf("Error initializing config: %s", err.Error())
2024-11-12 15:36:59 +01:00
}
2024-11-18 10:03:58 +01:00
err = initExtraRoute(extraRoutePath)
if err != nil {
t.Fatalf("Error creating extra routes file: %s", err.Error())
}
2024-11-18 12:58:49 +01:00
err = CheckConfig(configFile)
if err != nil {
t.Fatalf("Error checking config: %s", err.Error())
}
2024-11-15 15:42:02 +01:00
ctx := context.Background()
2024-10-27 06:10:27 +01:00
g := GatewayServer{}
2024-11-15 15:42:02 +01:00
gatewayServer, err := g.Config(configFile, ctx)
2024-10-27 06:10:27 +01:00
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) {
2024-11-25 17:41:23 +01:00
resp, err := s.Client().Get(s.URL + "/readyz")
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)
}
}
go func() {
2024-11-15 15:42:02 +01:00
err = gatewayServer.Start()
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
}