forked from TrueCloudLab/frostfs-s3-gw
[#387] middleware: Extend test coverage
Signed-off-by: Roman Loginov <r.loginov@yadro.com>
This commit is contained in:
parent
8a758293b9
commit
f4d174e740
5 changed files with 866 additions and 0 deletions
70
api/middleware/reqinfo_test.go
Normal file
70
api/middleware/reqinfo_test.go
Normal file
|
@ -0,0 +1,70 @@
|
|||
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")
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue