2017-02-10 09:21:32 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRequestID(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
req, _ := http.NewRequest(echo.GET, "/", nil)
|
|
|
|
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
|
|
|
}
|