2024-03-09 11:21:24 +02:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
|
|
|
|
|
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
|
|
|
|
2021-06-22 18:04:23 +02:00
|
|
|
// Custom generator and handler
|
|
|
|
customID := "customGenerator"
|
|
|
|
calledHandler := false
|
2017-03-17 18:22:58 +02:00
|
|
|
rid = RequestIDWithConfig(RequestIDConfig{
|
2021-06-22 18:04:23 +02:00
|
|
|
Generator: func() string { return customID },
|
|
|
|
RequestIDHandler: func(_ echo.Context, id string) {
|
|
|
|
calledHandler = true
|
|
|
|
assert.Equal(t, customID, id)
|
|
|
|
},
|
2017-03-17 18:22:58 +02:00
|
|
|
})
|
|
|
|
h = rid(handler)
|
|
|
|
h(c)
|
|
|
|
assert.Equal(t, rec.Header().Get(echo.HeaderXRequestID), "customGenerator")
|
2021-06-22 18:04:23 +02:00
|
|
|
assert.True(t, calledHandler)
|
2017-02-10 09:21:32 +02:00
|
|
|
}
|
2020-12-14 20:06:25 +02:00
|
|
|
|
|
|
|
func TestRequestID_IDNotAltered(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
req.Header.Add(echo.HeaderXRequestID, "<sample-request-id>")
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
handler := func(c echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "test")
|
|
|
|
}
|
|
|
|
|
|
|
|
rid := RequestIDWithConfig(RequestIDConfig{})
|
|
|
|
h := rid(handler)
|
|
|
|
_ = h(c)
|
|
|
|
assert.Equal(t, rec.Header().Get(echo.HeaderXRequestID), "<sample-request-id>")
|
|
|
|
}
|
2021-12-07 12:56:32 +02:00
|
|
|
|
|
|
|
func TestRequestIDConfigDifferentHeader(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
handler := func(c echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "test")
|
|
|
|
}
|
|
|
|
|
|
|
|
rid := RequestIDWithConfig(RequestIDConfig{TargetHeader: echo.HeaderXCorrelationID})
|
|
|
|
h := rid(handler)
|
|
|
|
h(c)
|
|
|
|
assert.Len(t, rec.Header().Get(echo.HeaderXCorrelationID), 32)
|
|
|
|
|
|
|
|
// Custom generator and handler
|
|
|
|
customID := "customGenerator"
|
|
|
|
calledHandler := false
|
|
|
|
rid = RequestIDWithConfig(RequestIDConfig{
|
|
|
|
Generator: func() string { return customID },
|
|
|
|
TargetHeader: echo.HeaderXCorrelationID,
|
|
|
|
RequestIDHandler: func(_ echo.Context, id string) {
|
|
|
|
calledHandler = true
|
|
|
|
assert.Equal(t, customID, id)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
h = rid(handler)
|
|
|
|
h(c)
|
|
|
|
assert.Equal(t, rec.Header().Get(echo.HeaderXCorrelationID), "customGenerator")
|
|
|
|
assert.True(t, calledHandler)
|
|
|
|
}
|