2021-02-01 06:37:56 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2022-12-06 06:41:09 +00:00
|
|
|
"github.com/TwiN/gatus/v5/config"
|
2024-05-10 02:56:16 +00:00
|
|
|
"github.com/TwiN/gatus/v5/config/endpoint"
|
2022-12-06 06:41:09 +00:00
|
|
|
"github.com/TwiN/gatus/v5/config/web"
|
2023-07-09 00:37:41 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2021-02-01 06:37:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHandle(t *testing.T) {
|
|
|
|
cfg := &config.Config{
|
2021-09-22 04:47:51 +00:00
|
|
|
Web: &web.Config{
|
2021-02-01 06:37:56 +00:00
|
|
|
Address: "0.0.0.0",
|
|
|
|
Port: rand.Intn(65534),
|
|
|
|
},
|
2024-05-10 02:56:16 +00:00
|
|
|
Endpoints: []*endpoint.Endpoint{
|
2021-02-01 06:37:56 +00:00
|
|
|
{
|
|
|
|
Name: "frontend",
|
|
|
|
Group: "core",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "backend",
|
|
|
|
Group: "core",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_ = os.Setenv("ROUTER_TEST", "true")
|
|
|
|
_ = os.Setenv("ENVIRONMENT", "dev")
|
2021-02-06 01:45:28 +00:00
|
|
|
defer os.Clearenv()
|
2022-07-29 00:07:53 +00:00
|
|
|
Handle(cfg)
|
2021-02-25 03:41:36 +00:00
|
|
|
defer Shutdown()
|
2023-07-09 00:37:41 +00:00
|
|
|
request := httptest.NewRequest("GET", "/health", http.NoBody)
|
|
|
|
response, err := app.Test(request)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if response.StatusCode != 200 {
|
2021-02-01 06:37:56 +00:00
|
|
|
t.Error("expected GET /health to return status code 200")
|
|
|
|
}
|
2023-07-09 00:37:41 +00:00
|
|
|
if app == nil {
|
2021-02-01 06:37:56 +00:00
|
|
|
t.Fatal("server should've been set (but because we set ROUTER_TEST, it shouldn't have been started)")
|
|
|
|
}
|
|
|
|
}
|
2021-02-06 01:45:28 +00:00
|
|
|
|
2023-04-22 19:22:09 +00: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 16:12:56 +00:00
|
|
|
},
|
|
|
|
}
|
2023-04-22 19:22:09 +00: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},
|
2024-05-10 02:56:16 +00:00
|
|
|
Endpoints: []*endpoint.Endpoint{
|
2023-04-22 19:22:09 +00:00
|
|
|
{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()
|
2023-07-09 00:37:41 +00:00
|
|
|
request := httptest.NewRequest("GET", "/health", http.NoBody)
|
|
|
|
response, err := app.Test(request)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if response.StatusCode != scenario.expectedStatusCode {
|
|
|
|
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, scenario.expectedStatusCode, response.StatusCode)
|
2023-04-22 19:22:09 +00:00
|
|
|
}
|
2023-07-09 00:37:41 +00:00
|
|
|
if app == nil {
|
2023-04-22 19:22:09 +00:00
|
|
|
t.Fatal("server should've been set (but because we set ROUTER_TEST, it shouldn't have been started)")
|
|
|
|
}
|
|
|
|
})
|
2023-04-22 16:12:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 01:45:28 +00:00
|
|
|
func TestShutdown(t *testing.T) {
|
|
|
|
// Pretend that we called controller.Handle(), which initializes the server variable
|
2023-07-09 00:37:41 +00:00
|
|
|
app = fiber.New()
|
2021-02-06 01:45:28 +00:00
|
|
|
Shutdown()
|
2023-07-09 00:37:41 +00:00
|
|
|
if app != nil {
|
2021-02-06 01:45:28 +00:00
|
|
|
t.Error("server should've been shut down")
|
|
|
|
}
|
|
|
|
}
|