Skip to content

Commit

Permalink
✨ feat(net/httpreq): add new option for send request, and add some ne…
Browse files Browse the repository at this point in the history
…w util func

- AppendQueryToURL
- AppendQueryToURLString
  • Loading branch information
inhere committed Mar 26, 2023
1 parent 247e751 commit 721b54a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
15 changes: 10 additions & 5 deletions netutil/httpreq/client.go
Expand Up @@ -3,6 +3,7 @@ package httpreq

import (
"bytes"
"context"
"io"
"net/http"
"strings"
Expand All @@ -13,7 +14,9 @@ import (

// ReqOption struct
type ReqOption struct {
// HeaderMap data. eg: traceid, parentid
// Method for request
Method string
// HeaderMap data. eg: traceid
HeaderMap map[string]string
// Timeout unit: ms
Timeout int
Expand All @@ -23,6 +26,8 @@ type ReqOption struct {
EncodeJSON bool
// Logger for request
Logger ReqLogger
// Context for request
Context context.Context
}

// Req alias of ReqClient
Expand Down Expand Up @@ -116,14 +121,14 @@ func (h *ReqClient) ContentType(cType string) *ReqClient {
return h.WithHeader(httpctype.Key, cType)
}

// BeforeSend add callback before send.
func (h *ReqClient) BeforeSend(fn func(req *http.Request)) *ReqClient {
// OnBeforeSend add callback before send.
func (h *ReqClient) OnBeforeSend(fn func(req *http.Request)) *ReqClient {
h.beforeSend = fn
return h
}

// AfterSend add callback after send.
func (h *ReqClient) AfterSend(fn func(resp *http.Response)) *ReqClient {
// OnAfterSend add callback after send.
func (h *ReqClient) OnAfterSend(fn func(resp *http.Response)) *ReqClient {
h.afterSend = fn
return h
}
Expand Down
4 changes: 2 additions & 2 deletions netutil/httpreq/client_test.go
Expand Up @@ -36,9 +36,9 @@ func TestHttpReq_Send(t *testing.T) {
}

func TestHttpReq_MustSend(t *testing.T) {
cli := httpreq.New().BeforeSend(func(req *http.Request) {
cli := httpreq.New().OnBeforeSend(func(req *http.Request) {
assert.Eq(t, http.MethodPost, req.Method)
}).AfterSend(func(resp *http.Response) {
}).OnAfterSend(func(resp *http.Response) {
bodyStr, _ := io.ReadAll(resp.Body)
assert.StrContains(t, string(bodyStr), "hi,goutil")
})
Expand Down
26 changes: 23 additions & 3 deletions netutil/httpreq/util.go
Expand Up @@ -131,8 +131,27 @@ func ToQueryValues(data any) url.Values {
return uv
}

// AppendQueryToURL appends the given query data to the given url.
func AppendQueryToURL(urlStr string, query url.Values) string {
// AppendQueryToURL appends the given query string to the given url.
func AppendQueryToURL(reqURL *url.URL, uv url.Values) error {
urlValues, err := url.ParseQuery(reqURL.RawQuery)
if err != nil {
return err
}

for key, values := range uv {
for _, value := range values {
urlValues.Add(key, value)
}
}

// url.Values format to a sorted "url encoded" string.
// e.g. "key=val&foo=bar"
reqURL.RawQuery = urlValues.Encode()
return nil
}

// AppendQueryToURLString appends the given query data to the given url.
func AppendQueryToURLString(urlStr string, query url.Values) string {
if len(query) == 0 {
return urlStr
}
Expand Down Expand Up @@ -209,6 +228,8 @@ func RequestToString(r *http.Request) string {
buf.WriteString(r.Method)
buf.WriteByte(' ')
buf.WriteString(r.URL.String())
buf.WriteByte(' ')
buf.WriteString(r.Proto)
buf.WriteByte('\n')

for key, values := range r.Header {
Expand All @@ -222,7 +243,6 @@ func RequestToString(r *http.Request) string {
buf.WriteByte('\n')
_, _ = buf.ReadFrom(r.Body)
}

return buf.String()
}

Expand Down
2 changes: 1 addition & 1 deletion netutil/httpreq/util_test.go
Expand Up @@ -56,7 +56,7 @@ func TestToQueryValues(t *testing.T) {
vs = httpreq.ToQueryValues(map[string]any{"field1": 234, "field2": "value2"})
assert.StrContains(t, vs.Encode(), "field1=234")
assert.Eq(t, "field1=234&field2=value2", vs.Encode())
assert.StrContains(t, "abc.com?field1=234&field2=value2", httpreq.AppendQueryToURL("abc.com", vs))
assert.StrContains(t, "abc.com?field1=234&field2=value2", httpreq.AppendQueryToURLString("abc.com", vs))

vs = httpreq.ToQueryValues(vs)
assert.Eq(t, "field1=234&field2=value2", vs.Encode())
Expand Down

0 comments on commit 721b54a

Please sign in to comment.