2021-02-01 07:37:56 +01:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2022-12-06 07:41:09 +01:00
|
|
|
"github.com/TwiN/gatus/v5/config"
|
|
|
|
"github.com/TwiN/gatus/v5/config/web"
|
|
|
|
"github.com/TwiN/gatus/v5/core"
|
2021-02-01 07:37:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHandle(t *testing.T) {
|
|
|
|
cfg := &config.Config{
|
2021-09-22 06:47:51 +02:00
|
|
|
Web: &web.Config{
|
2021-02-01 07:37:56 +01:00
|
|
|
Address: "0.0.0.0",
|
|
|
|
Port: rand.Intn(65534),
|
|
|
|
},
|
2021-10-23 22:47:12 +02:00
|
|
|
Endpoints: []*core.Endpoint{
|
2021-02-01 07:37:56 +01:00
|
|
|
{
|
|
|
|
Name: "frontend",
|
|
|
|
Group: "core",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "backend",
|
|
|
|
Group: "core",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_ = os.Setenv("ROUTER_TEST", "true")
|
|
|
|
_ = os.Setenv("ENVIRONMENT", "dev")
|
2021-02-06 02:45:28 +01:00
|
|
|
defer os.Clearenv()
|
2022-07-29 02:07:53 +02:00
|
|
|
Handle(cfg)
|
2021-02-25 04:41:36 +01:00
|
|
|
defer Shutdown()
|
2021-11-11 06:14:00 +01:00
|
|
|
request, _ := http.NewRequest("GET", "/health", http.NoBody)
|
2021-02-01 07:37:56 +01: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-06 02:45:28 +01:00
|
|
|
|
2023-04-22 21:22:09 +02:00
|
|
|
func TestHandleTLS(t *testing.T) {
|
|
|
|
scenarios := []struct {
|
|
|
|
name string
|
|
|
|
tls *web.TLSConfig
|
|
|
|
expectedStatusCode int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "good-tls-config",
|
|
|
|
tls: &web.TLSConfig{CertificateFile: "../testdata/cert.pem", PrivateKeyFile: "../testdata/cert.key"},
|
|
|
|
expectedStatusCode: 200,
|
2023-04-22 18:12:56 +02:00
|
|
|
},
|
|
|
|
}
|
2023-04-22 21:22:09 +02:00
|
|
|
for _, scenario := range scenarios {
|
|
|
|
t.Run(scenario.name, func(t *testing.T) {
|
|
|
|
cfg := &config.Config{
|
|
|
|
Web: &web.Config{Address: "0.0.0.0", Port: rand.Intn(65534), TLS: scenario.tls},
|
|
|
|
Endpoints: []*core.Endpoint{
|
|
|
|
{Name: "frontend", Group: "core"},
|
|
|
|
{Name: "backend", Group: "core"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := cfg.Web.ValidateAndSetDefaults(); err != nil {
|
|
|
|
t.Error("expected no error from web (TLS) validation, got", err)
|
|
|
|
}
|
|
|
|
_ = os.Setenv("ROUTER_TEST", "true")
|
|
|
|
_ = os.Setenv("ENVIRONMENT", "dev")
|
|
|
|
defer os.Clearenv()
|
|
|
|
Handle(cfg)
|
|
|
|
defer Shutdown()
|
|
|
|
request, _ := http.NewRequest("GET", "/health", http.NoBody)
|
|
|
|
responseRecorder := httptest.NewRecorder()
|
|
|
|
server.Handler.ServeHTTP(responseRecorder, request)
|
|
|
|
if responseRecorder.Code != scenario.expectedStatusCode {
|
|
|
|
t.Errorf("expected GET /health to return status code %d, got %d", scenario.expectedStatusCode, responseRecorder.Code)
|
|
|
|
}
|
|
|
|
if server == nil {
|
|
|
|
t.Fatal("server should've been set (but because we set ROUTER_TEST, it shouldn't have been started)")
|
|
|
|
}
|
|
|
|
})
|
2023-04-22 18:12:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 02:45:28 +01: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")
|
|
|
|
}
|
|
|
|
}
|