mirror of
https://github.com/TwiN/gatus.git
synced 2024-11-28 10:53:20 +01:00
2d3fe9795f
Gatus wasn't intended to be used as a library, but I have a use case now.
56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package controller
|
|
|
|
import (
|
|
"math/rand"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/TwinProduction/gatus/v3/config"
|
|
"github.com/TwinProduction/gatus/v3/config/web"
|
|
"github.com/TwinProduction/gatus/v3/core"
|
|
)
|
|
|
|
func TestHandle(t *testing.T) {
|
|
cfg := &config.Config{
|
|
Web: &web.Config{
|
|
Address: "0.0.0.0",
|
|
Port: rand.Intn(65534),
|
|
},
|
|
Services: []*core.Service{
|
|
{
|
|
Name: "frontend",
|
|
Group: "core",
|
|
},
|
|
{
|
|
Name: "backend",
|
|
Group: "core",
|
|
},
|
|
},
|
|
}
|
|
_ = os.Setenv("ROUTER_TEST", "true")
|
|
_ = os.Setenv("ENVIRONMENT", "dev")
|
|
defer os.Clearenv()
|
|
Handle(cfg.Security, cfg.Web, cfg.UI, cfg.Metrics)
|
|
defer Shutdown()
|
|
request, _ := http.NewRequest("GET", "/health", nil)
|
|
responseRecorder := httptest.NewRecorder()
|
|
server.Handler.ServeHTTP(responseRecorder, request)
|
|
if responseRecorder.Code != http.StatusOK {
|
|
t.Error("expected GET /health to return status code 200")
|
|
}
|
|
if server == nil {
|
|
t.Fatal("server should've been set (but because we set ROUTER_TEST, it shouldn't have been started)")
|
|
}
|
|
}
|
|
|
|
func TestShutdown(t *testing.T) {
|
|
// Pretend that we called controller.Handle(), which initializes the server variable
|
|
server = &http.Server{}
|
|
Shutdown()
|
|
if server != nil {
|
|
t.Error("server should've been shut down")
|
|
}
|
|
}
|