go-web-template/handlers/index_test.go
Valentin Brandl 5a03c74642
All checks were successful
Build / build (pull_request) Successful in 1m18s
Lint / lint (pull_request) Successful in 2m9s
Fix lints
2025-04-09 14:47:05 +02:00

54 lines
1.2 KiB
Go

package handlers_test
import (
"fmt"
"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 func() {
if err := res.Body.Close(); err != nil {
fmt.Println("Received error: ", err)
}
}()
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)
}
}