pgweb/pkg/api/logger_test.go
Dan Sosedoff afe431c94d
Request logging additions (request id, forwarded user) (#618)
* Add request-id logging
* Missing test file
* Add option to log forwarded user information if available via X-Forwarded-Header
* Format
2022-12-16 12:38:57 -06:00

32 lines
713 B
Go

package api
import (
"net/http"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func Test_getRequestID(t *testing.T) {
examples := []struct {
headers map[string]string
result string
}{
{map[string]string{}, ""},
{map[string]string{"X-Request-ID": "foo"}, "foo"},
{map[string]string{"x-request-id": "foo"}, "foo"},
{map[string]string{"x-request-id": "foo"}, "foo"},
{map[string]string{"x-request-id": "foo", "x-amzn-trace-id": "amz"}, "foo"},
}
for _, ex := range examples {
req := &http.Request{Header: http.Header{}}
for k, v := range ex.headers {
req.Header.Set(k, v)
}
assert.Equal(t, ex.result, getRequestID(&gin.Context{Request: req}))
}
}