forked from TrueCloudLab/frostfs-s3-gw
71 lines
1.7 KiB
Go
71 lines
1.7 KiB
Go
|
package middleware
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"net/http/httptest"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/stretchr/testify/require"
|
||
|
)
|
||
|
|
||
|
func TestGetSourceIP(t *testing.T) {
|
||
|
for _, tc := range []struct {
|
||
|
name string
|
||
|
req *http.Request
|
||
|
}{
|
||
|
{
|
||
|
name: "headers not set",
|
||
|
req: func() *http.Request {
|
||
|
request := httptest.NewRequest(http.MethodGet, "/test", nil)
|
||
|
request.RemoteAddr = "192.0.2.1:1234"
|
||
|
return request
|
||
|
}(),
|
||
|
},
|
||
|
{
|
||
|
name: "headers not set, and the port is not set",
|
||
|
req: func() *http.Request {
|
||
|
request := httptest.NewRequest(http.MethodGet, "/test", nil)
|
||
|
request.RemoteAddr = "192.0.2.1"
|
||
|
return request
|
||
|
}(),
|
||
|
},
|
||
|
{
|
||
|
name: "x-forwarded-for single-host header",
|
||
|
req: func() *http.Request {
|
||
|
request := httptest.NewRequest(http.MethodGet, "/test", nil)
|
||
|
request.Header.Set(xForwardedFor, "192.0.2.1")
|
||
|
return request
|
||
|
}(),
|
||
|
},
|
||
|
{
|
||
|
name: "x-forwarded-for header by multiple hosts",
|
||
|
req: func() *http.Request {
|
||
|
request := httptest.NewRequest(http.MethodGet, "/test", nil)
|
||
|
request.Header.Set(xForwardedFor, "192.0.2.1, 10.1.1.1")
|
||
|
return request
|
||
|
}(),
|
||
|
},
|
||
|
{
|
||
|
name: "x-real-ip header",
|
||
|
req: func() *http.Request {
|
||
|
request := httptest.NewRequest(http.MethodGet, "/test", nil)
|
||
|
request.Header.Set(xRealIP, "192.0.2.1")
|
||
|
return request
|
||
|
}(),
|
||
|
},
|
||
|
{
|
||
|
name: "forwarded header",
|
||
|
req: func() *http.Request {
|
||
|
request := httptest.NewRequest(http.MethodGet, "/test", nil)
|
||
|
request.Header.Set(forwarded, "for=192.0.2.1, 10.1.1.1; proto=https; by=192.0.2.4")
|
||
|
return request
|
||
|
}(),
|
||
|
},
|
||
|
} {
|
||
|
t.Run(tc.name, func(t *testing.T) {
|
||
|
actual := getSourceIP(tc.req)
|
||
|
require.Equal(t, actual, "192.0.2.1")
|
||
|
})
|
||
|
}
|
||
|
}
|