2017-02-10 09:21:32 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2019-01-30 12:56:56 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
2017-02-10 09:21:32 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRequestID(t *testing.T) {
|
|
|
|
e := echo.New()
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
2017-02-10 09:21:32 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
2017-03-17 18:22:58 +02:00
|
|
|
handler := func(c echo.Context) error {
|
2017-02-10 09:21:32 +02:00
|
|
|
return c.String(http.StatusOK, "test")
|
2017-03-17 18:22:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rid := RequestIDWithConfig(RequestIDConfig{})
|
|
|
|
h := rid(handler)
|
2017-02-10 09:21:32 +02:00
|
|
|
h(c)
|
2017-03-06 23:16:05 +02:00
|
|
|
assert.Len(t, rec.Header().Get(echo.HeaderXRequestID), 32)
|
2017-03-17 18:22:58 +02:00
|
|
|
|
|
|
|
// Custom generator
|
|
|
|
rid = RequestIDWithConfig(RequestIDConfig{
|
|
|
|
Generator: func() string { return "customGenerator" },
|
|
|
|
})
|
|
|
|
h = rid(handler)
|
|
|
|
h(c)
|
|
|
|
assert.Equal(t, rec.Header().Get(echo.HeaderXRequestID), "customGenerator")
|
2017-02-10 09:21:32 +02:00
|
|
|
}
|