27 lines
650 B
Go
27 lines
650 B
Go
|
|
package middleware
|
||
|
|
|
||
|
|
import (
|
||
|
|
"log/slog"
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"github.com/stretchr/testify/assert"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestRequestLogger(t *testing.T) {
|
||
|
|
called := false
|
||
|
|
h := RequestLogger(slog.New(slog.NewTextHandler(nil, nil)))(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
called = true
|
||
|
|
w.WriteHeader(http.StatusCreated)
|
||
|
|
_, _ = w.Write([]byte("ok"))
|
||
|
|
}))
|
||
|
|
req := httptest.NewRequest(http.MethodPost, "/q", strings.NewReader("body"))
|
||
|
|
rw := httptest.NewRecorder()
|
||
|
|
h.ServeHTTP(rw, req)
|
||
|
|
assert.True(t, called)
|
||
|
|
assert.Equal(t, http.StatusCreated, rw.Code)
|
||
|
|
assert.Equal(t, "ok", rw.Body.String())
|
||
|
|
}
|