2024-02-28 12:18:10 +00:00
|
|
|
package requestid
|
2024-02-27 15:17:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newRequest(t *testing.T) *http.Request {
|
2024-02-28 12:18:10 +00:00
|
|
|
t.Helper()
|
2024-02-27 15:17:09 +00:00
|
|
|
r, err := http.NewRequest(http.MethodGet, "https://example.com", http.NoBody)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2024-02-28 12:18:10 +00:00
|
|
|
func Test_Middleware(t *testing.T) {
|
2024-02-27 15:17:09 +00:00
|
|
|
requestWithID := newRequest(t)
|
|
|
|
requestWithID.Header.Set("X-Request-Id", "reqID")
|
2024-03-04 11:00:08 +00:00
|
|
|
|
2024-02-27 15:17:09 +00:00
|
|
|
requestWithoutID := newRequest(t)
|
2024-03-04 11:00:08 +00:00
|
|
|
|
2024-02-27 15:17:09 +00:00
|
|
|
requestWithEmptyHeader := newRequest(t)
|
|
|
|
requestWithEmptyHeader.Header.Set("X-Request-Id", "")
|
2024-03-04 11:00:08 +00:00
|
|
|
|
2024-02-27 15:17:09 +00:00
|
|
|
requestWithSmallstepID := newRequest(t)
|
|
|
|
requestWithSmallstepID.Header.Set("X-Smallstep-Id", "smallstepID")
|
2024-03-04 11:00:08 +00:00
|
|
|
|
2024-02-27 15:17:09 +00:00
|
|
|
tests := []struct {
|
2024-02-28 12:18:10 +00:00
|
|
|
name string
|
|
|
|
traceHeader string
|
|
|
|
next http.HandlerFunc
|
|
|
|
req *http.Request
|
2024-02-27 15:17:09 +00:00
|
|
|
}{
|
|
|
|
{
|
2024-02-28 12:18:10 +00:00
|
|
|
name: "default-request-id",
|
|
|
|
traceHeader: defaultTraceHeader,
|
|
|
|
next: func(w http.ResponseWriter, r *http.Request) {
|
2024-02-27 15:17:09 +00:00
|
|
|
assert.Empty(t, r.Header.Get("X-Smallstep-Id"))
|
|
|
|
assert.Equal(t, "reqID", r.Header.Get("X-Request-Id"))
|
2024-02-28 12:18:10 +00:00
|
|
|
reqID, ok := FromContext(r.Context())
|
2024-02-27 15:17:09 +00:00
|
|
|
if assert.True(t, ok) {
|
|
|
|
assert.Equal(t, "reqID", reqID)
|
|
|
|
}
|
2024-02-27 19:48:56 +00:00
|
|
|
assert.Equal(t, "reqID", w.Header().Get("X-Request-Id"))
|
2024-02-27 15:17:09 +00:00
|
|
|
},
|
|
|
|
req: requestWithID,
|
|
|
|
},
|
|
|
|
{
|
2024-02-28 12:18:10 +00:00
|
|
|
name: "no-request-id",
|
|
|
|
traceHeader: "X-Request-Id",
|
|
|
|
next: func(w http.ResponseWriter, r *http.Request) {
|
2024-02-27 15:17:09 +00:00
|
|
|
assert.Empty(t, r.Header.Get("X-Smallstep-Id"))
|
|
|
|
value := r.Header.Get("X-Request-Id")
|
|
|
|
assert.NotEmpty(t, value)
|
2024-02-28 12:18:10 +00:00
|
|
|
reqID, ok := FromContext(r.Context())
|
2024-02-27 15:17:09 +00:00
|
|
|
if assert.True(t, ok) {
|
|
|
|
assert.Equal(t, value, reqID)
|
|
|
|
}
|
2024-02-27 19:48:56 +00:00
|
|
|
assert.Equal(t, value, w.Header().Get("X-Request-Id"))
|
2024-02-27 15:17:09 +00:00
|
|
|
},
|
|
|
|
req: requestWithoutID,
|
|
|
|
},
|
|
|
|
{
|
2024-02-28 12:18:10 +00:00
|
|
|
name: "empty-header",
|
|
|
|
traceHeader: "",
|
|
|
|
next: func(w http.ResponseWriter, r *http.Request) {
|
2024-02-27 15:17:09 +00:00
|
|
|
assert.Empty(t, r.Header.Get("X-Request-Id"))
|
|
|
|
value := r.Header.Get("X-Smallstep-Id")
|
|
|
|
assert.NotEmpty(t, value)
|
2024-02-28 12:18:10 +00:00
|
|
|
reqID, ok := FromContext(r.Context())
|
2024-02-27 15:17:09 +00:00
|
|
|
if assert.True(t, ok) {
|
|
|
|
assert.Equal(t, value, reqID)
|
|
|
|
}
|
2024-02-27 19:48:56 +00:00
|
|
|
assert.Equal(t, value, w.Header().Get("X-Request-Id"))
|
2024-02-27 15:17:09 +00:00
|
|
|
},
|
|
|
|
req: requestWithEmptyHeader,
|
|
|
|
},
|
|
|
|
{
|
2024-02-28 12:18:10 +00:00
|
|
|
name: "fallback-header-name",
|
|
|
|
traceHeader: defaultTraceHeader,
|
|
|
|
next: func(w http.ResponseWriter, r *http.Request) {
|
2024-02-27 15:17:09 +00:00
|
|
|
assert.Empty(t, r.Header.Get("X-Request-Id"))
|
|
|
|
assert.Equal(t, "smallstepID", r.Header.Get("X-Smallstep-Id"))
|
2024-02-28 12:18:10 +00:00
|
|
|
reqID, ok := FromContext(r.Context())
|
2024-02-27 15:17:09 +00:00
|
|
|
if assert.True(t, ok) {
|
|
|
|
assert.Equal(t, "smallstepID", reqID)
|
|
|
|
}
|
2024-02-27 19:48:56 +00:00
|
|
|
assert.Equal(t, "smallstepID", w.Header().Get("X-Request-Id"))
|
2024-02-27 15:17:09 +00:00
|
|
|
},
|
|
|
|
req: requestWithSmallstepID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-02-28 12:18:10 +00:00
|
|
|
handler := New(tt.traceHeader).Middleware(tt.next)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(w, tt.req)
|
|
|
|
assert.NotEmpty(t, w.Header().Get("X-Request-Id"))
|
2024-02-27 15:17:09 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|