From 677b7f20e5af09068d55e152279a4f081040fa69 Mon Sep 17 00:00:00 2001 From: arraykeys Date: Wed, 20 Dec 2023 17:45:50 +0800 Subject: [PATCH] ghttp --- util/http/client_test.go | 3 ++- util/http/util.go | 10 ++++++++++ util/http/util_test.go | 13 +++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 util/http/util_test.go diff --git a/util/http/client_test.go b/util/http/client_test.go index 53329a02..48241062 100644 --- a/util/http/client_test.go +++ b/util/http/client_test.go @@ -140,8 +140,9 @@ func TestHTTPClient_Get6(t *testing.T) { assert := assert2.New(t) client := NewHTTPClient() client.SetClientCert(cert, key) - body, _, _, _ := client.Get(httpsServerURL2+"/hello", time.Second, nil, map[string]string{"token": "200"}) + body, _, resp, _ := client.Get(httpsServerURL2+"/hello", time.Second, nil, map[string]string{"token": "200"}) assert.Contains(string(body), "hello") + assert.Contains(string(GetResponseBody(resp)), "hello") } func TestHTTPClient_SetProxyFromEnv(t *testing.T) { diff --git a/util/http/util.go b/util/http/util.go index bb6443e7..601bffeb 100644 --- a/util/http/util.go +++ b/util/http/util.go @@ -118,3 +118,13 @@ func GetResponseBody(resp *http.Response) []byte { b, _ := GetResponseBodyE(resp) return b } + +func GetContent(_url string, timeout time.Duration) []byte { + d, _ := GetContentE(_url, timeout) + return d +} + +func GetContentE(_url string, timeout time.Duration) ([]byte, error) { + d, _, e := Download(_url, timeout, nil, nil) + return d, e +} diff --git a/util/http/util_test.go b/util/http/util_test.go new file mode 100644 index 00000000..f9982532 --- /dev/null +++ b/util/http/util_test.go @@ -0,0 +1,13 @@ +package ghttp + +import ( + "github.com/stretchr/testify/assert" + "testing" + "time" +) + +func TestGetContents(t *testing.T) { + assert.Equal(t, "hello", string(GetContent(httpServerURL+"/hello", time.Second))) + _, e := GetContentE("http://none/none", time.Second) + assert.NotNil(t, e) +}