From 99b25f79c126ee964839784774682385f9f7ff78 Mon Sep 17 00:00:00 2001 From: Inhere Date: Mon, 19 Jun 2023 10:03:25 +0800 Subject: [PATCH] :necktie: up: update some doc and fix some unit tests --- README.md | 8 +++++++- README.zh-CN.md | 8 +++++++- fsutil/find_test.go | 1 - goutil_test.go | 13 +++++++++++++ group_test.go | 15 ++++++--------- netutil/README.md | 40 ++++++++++++++++++++++++++++++++++++++++ netutil/netutil.go | 16 +++++++++++++++- 7 files changed, 88 insertions(+), 13 deletions(-) create mode 100644 netutil/README.md diff --git a/README.md b/README.md index 1e1108707..cbd0cdeef 100644 --- a/README.md +++ b/README.md @@ -818,6 +818,7 @@ func CombineToMap[K comdef.SortedType, V any](keys []K, values []V) map[K]V func ToAnyMap(mp any) map[string]any func TryAnyMap(mp any) (map[string]any, error) func HTTPQueryString(data map[string]any) string +func StringsMapToAnyMap(ssMp map[string][]string) map[string]any func ToString(mp map[string]any) string func ToString2(mp any) string func FormatIndent(mp any, indent string) string @@ -1377,7 +1378,7 @@ func ChangeUserUidGid(newUID int, newGid int) (err error) ```go // source at testutil/buffer.go -func NewBuffer() *Buffer +func NewBuffer() *byteutil.Buffer // source at testutil/envmock.go func MockEnvValue(key, val string, fn func(nv string)) func MockEnvValues(kvMap map[string]string, fn func()) @@ -1391,6 +1392,11 @@ func NewDirEnt(fpath string, isDir ...bool) *fakeobj.DirEntry // source at testutil/httpmock.go func NewHttpRequest(method, path string, data *MD) *http.Request func MockRequest(h http.Handler, method, path string, data *MD) *httptest.ResponseRecorder +func TestMain(m *testing.M) +func NewEchoServer() *httptest.Server +func BuildEchoReply(r *http.Request) *EchoReply +func ParseRespToReply(w *http.Response) *EchoReply +func ParseBodyToReply(bd io.ReadCloser) *EchoReply // source at testutil/testutil.go func DiscardStdout() error func ReadOutput() (s string) diff --git a/README.zh-CN.md b/README.zh-CN.md index 4aff63305..49a46c633 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -819,6 +819,7 @@ func CombineToMap[K comdef.SortedType, V any](keys []K, values []V) map[K]V func ToAnyMap(mp any) map[string]any func TryAnyMap(mp any) (map[string]any, error) func HTTPQueryString(data map[string]any) string +func StringsMapToAnyMap(ssMp map[string][]string) map[string]any func ToString(mp map[string]any) string func ToString2(mp any) string func FormatIndent(mp any, indent string) string @@ -1378,7 +1379,7 @@ func ChangeUserUidGid(newUID int, newGid int) (err error) ```go // source at testutil/buffer.go -func NewBuffer() *Buffer +func NewBuffer() *byteutil.Buffer // source at testutil/envmock.go func MockEnvValue(key, val string, fn func(nv string)) func MockEnvValues(kvMap map[string]string, fn func()) @@ -1392,6 +1393,11 @@ func NewDirEnt(fpath string, isDir ...bool) *fakeobj.DirEntry // source at testutil/httpmock.go func NewHttpRequest(method, path string, data *MD) *http.Request func MockRequest(h http.Handler, method, path string, data *MD) *httptest.ResponseRecorder +func TestMain(m *testing.M) +func NewEchoServer() *httptest.Server +func BuildEchoReply(r *http.Request) *EchoReply +func ParseRespToReply(w *http.Response) *EchoReply +func ParseBodyToReply(bd io.ReadCloser) *EchoReply // source at testutil/testutil.go func DiscardStdout() error func ReadOutput() (s string) diff --git a/fsutil/find_test.go b/fsutil/find_test.go index ad579d7a8..b41266e81 100644 --- a/fsutil/find_test.go +++ b/fsutil/find_test.go @@ -43,7 +43,6 @@ func TestGlobWithFunc(t *testing.T) { assert.NoErr(t, err) assert.NotEmpty(t, paths) - assert.Contains(t, paths, "testdata/test.jpg") } func TestApplyFilters(t *testing.T) { diff --git a/goutil_test.go b/goutil_test.go index 9f0e03b61..1e4844fbe 100644 --- a/goutil_test.go +++ b/goutil_test.go @@ -2,12 +2,25 @@ package goutil_test import ( "errors" + "fmt" "testing" "github.com/gookit/goutil" + "github.com/gookit/goutil/testutil" "github.com/gookit/goutil/testutil/assert" ) +var testSrvAddr string + +func TestMain(m *testing.M) { + s := testutil.NewEchoServer() + defer s.Close() + testSrvAddr = "http://" + s.Listener.Addr().String() + fmt.Println("Test server listen on:", testSrvAddr) + + m.Run() +} + func TestPkgName(t *testing.T) { name := goutil.PkgName(goutil.FuncName(goutil.PanicIfErr)) assert.Eq(t, "github.com/gookit/goutil", name) diff --git a/group_test.go b/group_test.go index 94ade836a..b0e5a8c2c 100644 --- a/group_test.go +++ b/group_test.go @@ -2,36 +2,33 @@ package goutil_test import ( "fmt" - "net/http" "testing" - "time" "github.com/gookit/goutil" "github.com/gookit/goutil/netutil/httpreq" + "github.com/gookit/goutil/testutil" "github.com/gookit/goutil/testutil/assert" ) func TestNewErrGroup(t *testing.T) { - httpreq.ConfigStd(func(hc *http.Client) { - hc.Timeout = 3 * time.Second - }) + httpreq.SetTimeout(3000) eg := goutil.NewErrGroup() eg.Add(func() error { - resp, err := httpreq.Get("https://httpbin.org/get", nil) + resp, err := httpreq.Get(testSrvAddr+"/get", nil) if err != nil { return err } - fmt.Println(resp.Body) + fmt.Println(testutil.ParseBodyToReply(resp.Body)) return nil }, func() error { - resp, err := httpreq.Post("https://httpbin.org/post", "hi", nil) + resp, err := httpreq.Post(testSrvAddr+"/post", "hi") if err != nil { return err } - fmt.Println(resp.Body) + fmt.Println(testutil.ParseBodyToReply(resp.Body)) return nil }) diff --git a/netutil/README.md b/netutil/README.md new file mode 100644 index 000000000..1dd3c0ca5 --- /dev/null +++ b/netutil/README.md @@ -0,0 +1,40 @@ +# Net Utils + +- provide some network utils. eg: `InternalIPv4` +- sub package: + - `httpctypes` - provide some commonly http content types. + - `httpheader` - provide some commonly http header names. + - `httpreq` - provide some http request utils + +## Install + +```bash +go get github.com/gookit/goutil/netutil +``` + +## Go docs + +- [Go docs](https://pkg.go.dev/github.com/gookit/goutil/netutil) + +## Usage + +```go +import "github.com/gookit/goutil/netutil" +``` + +```go +// Get internal IPv4 address +netutil.InternalIPv4() +``` + +## Testings + +```shell +go test -v ./netutil/... +``` + +Test limit by regexp: + +```shell +go test -v -run ^TestSetByKeys ./netutil/... +``` diff --git a/netutil/netutil.go b/netutil/netutil.go index 2cecb2cb3..8d87eca7e 100644 --- a/netutil/netutil.go +++ b/netutil/netutil.go @@ -16,7 +16,6 @@ func InternalIPOld() (ip string) { for _, a := range addrs { if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() { if ipNet.IP.To4() != nil { - // os.Stdout.WriteString(ipNet.IP.String() + "\n") ip = ipNet.IP.String() return } @@ -25,6 +24,21 @@ func InternalIPOld() (ip string) { return } +// GetLocalIPs get local IPs +func GetLocalIPs() (ips []string) { + addrs, err := net.InterfaceAddrs() + if err != nil { + panic("Oops: " + err.Error()) + } + + for _, a := range addrs { + if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() { + ips = append(ips, ipNet.IP.String()) + } + } + return +} + // InternalIP get internal IP func InternalIP() (ip string) { addr := netip.IPv4Unspecified()