gatus/controller/handler/handler_test.go

77 lines
1.8 KiB
Go
Raw Normal View History

2021-09-13 00:39:09 +02:00
package handler
import (
"net/http"
"net/http/httptest"
"testing"
2022-12-06 07:41:09 +01:00
"github.com/TwiN/gatus/v5/config"
2021-09-13 00:39:09 +02:00
)
func TestCreateRouter(t *testing.T) {
router := CreateRouter(&config.Config{Metrics: true})
2021-09-13 00:39:09 +02:00
type Scenario struct {
Name string
Path string
ExpectedCode int
Gzip bool
}
scenarios := []Scenario{
{
Name: "health",
Path: "/health",
ExpectedCode: http.StatusOK,
},
{
Name: "metrics",
Path: "/metrics",
ExpectedCode: http.StatusOK,
},
{
Name: "favicon.ico",
Path: "/favicon.ico",
ExpectedCode: http.StatusOK,
},
{
Name: "app.js",
2021-09-13 00:39:09 +02:00
Path: "/js/app.js",
ExpectedCode: http.StatusOK,
},
{
Name: "app.js-gzipped",
2021-09-13 00:39:09 +02:00
Path: "/js/app.js",
ExpectedCode: http.StatusOK,
Gzip: true,
},
{
Name: "chunk-vendors.js",
Path: "/js/chunk-vendors.js",
ExpectedCode: http.StatusOK,
},
{
Name: "chunk-vendors.js-gzipped",
Path: "/js/chunk-vendors.js",
ExpectedCode: http.StatusOK,
Gzip: true,
},
2021-09-13 00:39:09 +02:00
{
Name: "index-redirect",
Path: "/index.html",
ExpectedCode: http.StatusMovedPermanently,
},
}
for _, scenario := range scenarios {
t.Run(scenario.Name, func(t *testing.T) {
request, _ := http.NewRequest("GET", scenario.Path, http.NoBody)
2021-09-13 00:39:09 +02:00
if scenario.Gzip {
request.Header.Set("Accept-Encoding", "gzip")
}
responseRecorder := httptest.NewRecorder()
router.ServeHTTP(responseRecorder, request)
if responseRecorder.Code != scenario.ExpectedCode {
t.Errorf("%s %s should have returned %d, but returned %d instead", request.Method, request.URL, scenario.ExpectedCode, responseRecorder.Code)
}
})
}
}