Files
gatus/controller/controller_test.go
T

55 lines
1.2 KiB
Go
Raw Normal View History

2021-02-01 01:37:56 -05:00
package controller
import (
"math/rand"
"net/http"
"net/http/httptest"
"os"
"testing"
2022-06-20 21:25:14 -04:00
"github.com/TwiN/gatus/v4/config"
"github.com/TwiN/gatus/v4/config/web"
"github.com/TwiN/gatus/v4/core"
2021-02-01 01:37:56 -05:00
)
func TestHandle(t *testing.T) {
cfg := &config.Config{
Web: &web.Config{
2021-02-01 01:37:56 -05:00
Address: "0.0.0.0",
Port: rand.Intn(65534),
},
2021-10-23 16:47:12 -04:00
Endpoints: []*core.Endpoint{
2021-02-01 01:37:56 -05:00
{
Name: "frontend",
Group: "core",
},
{
Name: "backend",
Group: "core",
},
},
}
_ = os.Setenv("ROUTER_TEST", "true")
_ = os.Setenv("ENVIRONMENT", "dev")
2021-02-05 20:45:28 -05:00
defer os.Clearenv()
Handle(cfg)
defer Shutdown()
2021-11-11 00:14:00 -05:00
request, _ := http.NewRequest("GET", "/health", http.NoBody)
2021-02-01 01:37:56 -05:00
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)")
}
}
2021-02-05 20:45:28 -05:00
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")
}
}