go-zero/rest/httpx/util_test.go
2021-10-20 17:50:01 +08:00

26 lines
514 B
Go

package httpx
import (
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetRemoteAddr(t *testing.T) {
host := "8.8.8.8"
r, err := http.NewRequest(http.MethodGet, "/", strings.NewReader(""))
assert.Nil(t, err)
r.Header.Set(xForwardedFor, host)
assert.Equal(t, host, GetRemoteAddr(r))
}
func TestGetRemoteAddrNoHeader(t *testing.T) {
r, err := http.NewRequest(http.MethodGet, "/", strings.NewReader(""))
assert.Nil(t, err)
assert.True(t, len(GetRemoteAddr(r)) == 0)
}