2020-08-12 12:25:52 +08:00
|
|
|
package httpx
|
2020-07-29 18:00:04 +08:00
|
|
|
|
|
|
|
|
import (
|
2024-12-23 00:56:20 +08:00
|
|
|
"fmt"
|
2020-07-29 18:00:04 +08:00
|
|
|
"net/http"
|
2024-12-23 00:56:20 +08:00
|
|
|
"net/url"
|
2020-07-29 18:00:04 +08:00
|
|
|
"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)
|
|
|
|
|
|
2021-02-26 16:27:04 +08:00
|
|
|
r.Header.Set(xForwardedFor, host)
|
2020-07-29 18:00:04 +08:00
|
|
|
assert.Equal(t, host, GetRemoteAddr(r))
|
|
|
|
|
}
|
2021-10-20 17:50:01 +08:00
|
|
|
|
|
|
|
|
func TestGetRemoteAddrNoHeader(t *testing.T) {
|
|
|
|
|
r, err := http.NewRequest(http.MethodGet, "/", strings.NewReader(""))
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
|
|
assert.True(t, len(GetRemoteAddr(r)) == 0)
|
|
|
|
|
}
|
2024-12-23 00:56:20 +08:00
|
|
|
|
|
|
|
|
func TestGetFormValues_TooManyValues(t *testing.T) {
|
|
|
|
|
form := url.Values{}
|
|
|
|
|
|
|
|
|
|
// Add more values than the limit
|
|
|
|
|
for i := 0; i < maxFormParamCount+10; i++ {
|
|
|
|
|
form.Add("param", fmt.Sprintf("value%d", i))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a new request with the form data
|
|
|
|
|
req, err := http.NewRequest("POST", "/test", strings.NewReader(form.Encode()))
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Set the content type for form data
|
|
|
|
|
req.Header.Set(ContentType, "application/x-www-form-urlencoded")
|
|
|
|
|
|
|
|
|
|
_, err = GetFormValues(req)
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
assert.Contains(t, err.Error(), "too many form values")
|
|
|
|
|
}
|