2021-09-13 00:39:09 +02:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2022-07-29 02:07:53 +02:00
|
|
|
|
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) {
|
2022-10-10 03:33:31 +02:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
{
|
2022-10-10 03:33:31 +02:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
{
|
2022-10-10 03:33:31 +02:00
|
|
|
Name: "app.js-gzipped",
|
2021-09-13 00:39:09 +02:00
|
|
|
Path: "/js/app.js",
|
|
|
|
ExpectedCode: http.StatusOK,
|
|
|
|
Gzip: true,
|
|
|
|
},
|
2022-10-10 03:33:31 +02:00
|
|
|
{
|
|
|
|
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) {
|
2021-11-11 06:14:00 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|