Skip to content

Commit

Permalink
✨ feat(net/hreq): add more util func and simple basic-auth build
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 22, 2023
1 parent 7670393 commit ea450e9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
4 changes: 3 additions & 1 deletion netutil/httpreq/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ func (h *ReqClient) BaseURL(baseURL string) *ReqClient {

// Method with custom method
func (h *ReqClient) Method(method string) *ReqClient {
h.method = method
if method != "" {
h.method = method
}
return h
}

Expand Down
37 changes: 36 additions & 1 deletion netutil/httpreq/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ import (
"github.com/gookit/goutil/strutil"
)

// BasicAuthConf struct
type BasicAuthConf struct {
Username string `json:"username"`
Password string `json:"password"`
}

// IsValid value
func (ba *BasicAuthConf) IsValid() bool {
return ba.Password != "" && ba.Username != ""
}

// Value build to auth header "Authorization".
func (ba *BasicAuthConf) Value() string {
return BuildBasicAuth(ba.Username, ba.Password)
}

// String build to auth header "Authorization".
func (ba *BasicAuthConf) String() string {
return ba.Username + ":" + ba.Password
}

// IsOK check response status code is 200
func IsOK(statusCode int) bool {
return statusCode == http.StatusOK
Expand Down Expand Up @@ -51,6 +72,8 @@ func IsServerError(statusCode int) bool {
}

// BuildBasicAuth returns the base64 encoded username:password for basic auth.
// Then set to header "Authorization".
//
// copied from net/http.
func BuildBasicAuth(username, password string) string {
auth := username + ":" + password
Expand Down Expand Up @@ -87,7 +110,7 @@ func HeaderToStringMap(rh http.Header) map[string]string {
return mp
}

// ToQueryValues convert string-map to url.Values
// ToQueryValues convert string-map or any-map to url.Values
func ToQueryValues(data any) url.Values {
// use url.Values directly if we have it
if uv, ok := data.(url.Values); ok {
Expand All @@ -108,6 +131,18 @@ 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 {
if len(query) == 0 {
return urlStr
}

if strings.ContainsRune(urlStr, '?') {
return urlStr + "&" + query.Encode()
}
return urlStr + "?" + query.Encode()
}

// IsNoBodyMethod check
func IsNoBodyMethod(method string) bool {
return method != "POST" && method != "PUT" && method != "PATCH"
Expand Down
16 changes: 16 additions & 0 deletions netutil/httpreq/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,20 @@ import (
func TestBuildBasicAuth(t *testing.T) {
val := httpreq.BuildBasicAuth("inhere", "abcd&123")

assert.Eq(t, "Basic aW5oZXJlOmFiY2QmMTIz", val)
assert.Contains(t, val, "Basic ")
}

func TestBasicAuthConf_Value(t *testing.T) {
bac := httpreq.BasicAuthConf{
Username: "user",
Password: "pass",
}
assert.Eq(t, "user:pass", bac.String())
assert.Eq(t, "Basic dXNlcjpwYXNz", bac.Value())
assert.True(t, bac.IsValid())
}

func TestAddHeaders(t *testing.T) {
req, err := http.NewRequest("GET", "inhere.xyz", nil)
assert.NoErr(t, err)
Expand Down Expand Up @@ -44,6 +55,11 @@ 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))

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

func TestRequestToString(t *testing.T) {
Expand Down

0 comments on commit ea450e9

Please sign in to comment.