Marvin Preuss
17fcd55b85
i discovered similarities in hlog. so i refactored this lib to use hlog to be more compatible. logginghandler.Handler setups all Handlers from hlog that which has similarities. only the proto handler is missing. a PR is open for hlog.
43 lines
960 B
Go
43 lines
960 B
Go
package logginghandler_test
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
"github.com/stretchr/testify/assert"
|
|
"go.xsfx.dev/logginghandler"
|
|
)
|
|
|
|
func Example() {
|
|
logger := log.With().Logger()
|
|
|
|
handler := logginghandler.Handler(logger)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
logger := logginghandler.Logger(r)
|
|
|
|
logger.Info().Msg("this is a request")
|
|
}))
|
|
|
|
http.Handle("/", handler)
|
|
log.Fatal().Msg(http.ListenAndServe(":5000", nil).Error())
|
|
}
|
|
|
|
func testHandler(w http.ResponseWriter, r *http.Request) {
|
|
}
|
|
|
|
func TestUUID(t *testing.T) {
|
|
t.Parallel()
|
|
assert := assert.New(t)
|
|
req, err := http.NewRequestWithContext(context.Background(), "GET", "/test", nil)
|
|
assert.NoError(err)
|
|
|
|
rr := httptest.NewRecorder()
|
|
handler := logginghandler.Handler(log.With().Logger())(http.HandlerFunc(testHandler))
|
|
|
|
handler.ServeHTTP(rr, req)
|
|
|
|
assert.NotEmpty(rr.Header().Get("X-Request-ID"))
|
|
}
|