go-web-template/handlers/index_test.go

49 lines
1.1 KiB
Go
Raw Normal View History

2024-08-25 16:40:48 +02:00
package handlers_test
import (
"net/http"
"net/http/httptest"
"testing"
"git.vbrandl.net/vbrandl/go-web-template/assets"
"git.vbrandl.net/vbrandl/go-web-template/common"
"git.vbrandl.net/vbrandl/go-web-template/handlers"
"git.vbrandl.net/vbrandl/go-web-template/service"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func PrepareApplication() handlers.Application {
config := common.NewConfig()
ser := service.New(config)
zerolog.SetGlobalLevel(config.LogLevel())
app, err := handlers.NewApplication(ser, assets.Static)
if err != nil {
log.Fatal().Err(err).Msg("failed creating application")
}
return app
}
func TestIndex(t *testing.T) {
app := PrepareApplication()
req := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
app.Handler().ServeHTTP(w, req)
// app.Index(w, req)
res := w.Result()
defer res.Body.Close()
if res.StatusCode != 200 {
t.Errorf("expected status to be 200, got %d", res.StatusCode)
}
reqId := res.Header.Get("x-request-id")
if reqId == "" {
t.Errorf("expected `x-request-id` header to be present, got %s", reqId)
}
}