middleware/real_ip_test.go

119 lines
2.9 KiB
Go
Raw Permalink Normal View History

2020-11-07 11:59:33 +00:00
package middleware
import (
2023-12-25 00:47:12 +00:00
"io"
2020-11-07 11:59:33 +00:00
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var realIPTestHandler = func(t *testing.T) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.RemoteAddr, "3.3.3.3")
_, err := w.Write([]byte("resp"))
require.NoError(t, err)
})
}
func TestRealIPXRealIP(t *testing.T) {
server := httptest.NewServer(RealIP(realIPTestHandler(t)))
defer server.Close()
client := http.Client{}
2023-06-25 20:10:30 +00:00
req, err := http.NewRequest("GET", server.URL, http.NoBody)
2020-11-07 11:59:33 +00:00
require.Nil(t, err)
req.Header.Set("X-Real-IP", "3.3.3.3")
res, err := client.Do(req)
require.NoError(t, err)
defer res.Body.Close()
assert.Equal(t, http.StatusOK, res.StatusCode)
2023-12-25 00:47:12 +00:00
b, err := io.ReadAll(res.Body)
2020-11-07 11:59:33 +00:00
assert.NoError(t, err)
assert.Equal(t, "resp", string(b))
}
func TestRealIPXForwardedFor(t *testing.T) {
server := httptest.NewServer(RealIP(realIPTestHandler(t)))
defer server.Close()
client := http.Client{}
2023-06-25 20:10:30 +00:00
req, err := http.NewRequest("GET", server.URL, http.NoBody)
2020-11-07 11:59:33 +00:00
require.Nil(t, err)
req.Header.Set("X-Forwarded-For", "3.3.3.3")
res, err := client.Do(req)
require.NoError(t, err)
defer res.Body.Close()
assert.Equal(t, http.StatusOK, res.StatusCode)
2023-06-25 20:10:30 +00:00
req, err = http.NewRequest("GET", server.URL, http.NoBody)
2020-11-07 11:59:33 +00:00
require.Nil(t, err)
req.Header.Set("X-Forwarded-For", "3.3.3.3, 4.4.4.4, 5.5.5.5")
res, err = client.Do(req)
require.NoError(t, err)
defer res.Body.Close()
assert.Equal(t, http.StatusOK, res.StatusCode)
}
func TestRealIPBothHeaders(t *testing.T) {
server := httptest.NewServer(RealIP(realIPTestHandler(t)))
defer server.Close()
client := http.Client{}
2023-06-25 20:10:30 +00:00
req, err := http.NewRequest("GET", server.URL, http.NoBody)
2020-11-07 11:59:33 +00:00
require.Nil(t, err)
req.Header.Set("X-Real-IP", "3.3.3.3")
req.Header.Set("X-Forwarded-For", "4.4.4.4")
res, err := client.Do(req)
require.NoError(t, err)
defer res.Body.Close()
assert.Equal(t, http.StatusOK, res.StatusCode)
}
func TestRealIPNoHeaders(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, strings.Split(r.RemoteAddr, ":")[0], "127.0.0.1")
_, err := w.Write([]byte("resp"))
require.NoError(t, err)
})
server := httptest.NewServer(RealIP(handler))
defer server.Close()
res, err := http.Get(server.URL)
require.NoError(t, err)
defer res.Body.Close()
assert.Equal(t, http.StatusOK, res.StatusCode)
}
2022-05-12 11:29:33 +00:00
func TestRealIPContext(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, strings.Split(r.RemoteAddr, ":")[0], "127.0.0.1")
assert.Equal(t, r.Context().Value(RealIPCtxKey), "127.0.0.1")
_, err := w.Write([]byte("resp"))
require.NoError(t, err)
})
server := httptest.NewServer(RealIP(handler))
defer server.Close()
res, err := http.Get(server.URL)
require.NoError(t, err)
defer res.Body.Close()
assert.Equal(t, http.StatusOK, res.StatusCode)
}