nezha/cmd/agent/monitor/myip_test.go

33 lines
717 B
Go
Raw Normal View History

2022-03-31 10:45:26 -04:00
package monitor
import (
2022-10-18 11:40:01 -04:00
"io"
2022-03-31 10:45:26 -04:00
"testing"
"github.com/stretchr/testify/assert"
"github.com/naiba/nezha/pkg/utils"
)
func TestGeoIPApi(t *testing.T) {
for i := 0; i < len(geoIPApiList); i++ {
resp, err := httpGetWithUA(httpClientV4, geoIPApiList[i])
assert.Nil(t, err)
2022-10-18 11:40:01 -04:00
body, err := io.ReadAll(resp.Body)
2022-03-31 10:45:26 -04:00
assert.Nil(t, err)
resp.Body.Close()
var ip geoIP
2022-03-31 10:59:07 -04:00
err = ip.Unmarshal(body)
2022-03-31 10:45:26 -04:00
assert.Nil(t, err)
2022-03-31 10:59:07 -04:00
t.Logf("%s %s %s", geoIPApiList[i], ip.CountryCode, utils.IPDesensitize(ip.IP))
assert.True(t, ip.IP != "")
assert.True(t, ip.CountryCode != "")
2022-03-31 10:45:26 -04:00
}
}
func TestFetchGeoIP(t *testing.T) {
ip := fetchGeoIP(geoIPApiList, false)
assert.NotEmpty(t, ip.IP)
2022-03-31 10:59:07 -04:00
assert.NotEmpty(t, ip.CountryCode)
2022-03-31 10:45:26 -04:00
}