Skip to content

Commit 9f3f541

Browse files
committed
👔 up: httpreq - add new func IsValidMethod
1 parent 1af9f30 commit 9f3f541

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

netutil/httpreq/client.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type Client struct {
1818
method string
1919
baseURL string
2020
timeout int // unit: ms
21-
// custom set headers
21+
// custom set default headers
2222
headerMap map[string]string
2323

2424
// before send callback
@@ -217,12 +217,11 @@ func (h *Client) MustSend(method, url string, optFns ...OptionFn) *http.Response
217217

218218
// SendWithOpt request and return http response
219219
func (h *Client) SendWithOpt(url string, opt *Option) (*http.Response, error) {
220-
cli := h
221-
if len(cli.baseURL) > 0 {
220+
if len(h.baseURL) > 0 {
222221
if !strings.HasPrefix(url, "http") {
223-
url = cli.baseURL + url
222+
url = h.baseURL + url
224223
} else if len(url) == 0 {
225-
url = cli.baseURL
224+
url = h.baseURL
226225
}
227226
}
228227

@@ -233,7 +232,7 @@ func (h *Client) SendWithOpt(url string, opt *Option) (*http.Response, error) {
233232
}
234233

235234
// create request
236-
method := strings.ToUpper(strutil.OrElse(opt.Method, cli.method))
235+
method := strings.ToUpper(strutil.OrElse(opt.Method, h.method))
237236

238237
if opt.Data != nil {
239238
if IsNoBodyMethod(method) {

netutil/httpreq/httpreq.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ import (
99
"github.com/gookit/goutil/netutil/httpctype"
1010
)
1111

12+
// ValidMethods valid http methods
13+
var ValidMethods = []string{
14+
http.MethodGet,
15+
http.MethodPost,
16+
http.MethodPut,
17+
http.MethodPatch,
18+
http.MethodDelete,
19+
http.MethodConnect,
20+
http.MethodHead,
21+
http.MethodOptions,
22+
http.MethodTrace,
23+
}
24+
1225
// AfterSendFn callback func
1326
type AfterSendFn func(resp *http.Response, err error)
1427

netutil/httpreq/util.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ func IsNoBodyMethod(method string) bool {
7272
return method != "POST" && method != "PUT" && method != "PATCH"
7373
}
7474

75+
// IsValidMethod check method is valid
76+
func IsValidMethod(method string) bool {
77+
method = strings.ToUpper(method)
78+
return http.MethodGet == method ||
79+
http.MethodPost == method ||
80+
http.MethodPut == method ||
81+
http.MethodPatch == method ||
82+
http.MethodDelete == method ||
83+
http.MethodConnect == method ||
84+
http.MethodHead == method ||
85+
http.MethodOptions == method ||
86+
http.MethodTrace == method
87+
}
88+
7589
// BuildBasicAuth returns the base64 encoded username:password for basic auth.
7690
// Then set to header "Authorization".
7791
//
@@ -101,14 +115,14 @@ func SetHeaders(req *http.Request, headers ...http.Header) {
101115
}
102116
}
103117

104-
// AddHeaderMap to reqeust instance.
118+
// AddHeaderMap to request instance.
105119
func AddHeaderMap(req *http.Request, headerMap map[string]string) {
106120
for k, v := range headerMap {
107121
req.Header.Add(k, v)
108122
}
109123
}
110124

111-
// SetHeaderMap to reqeust instance.
125+
// SetHeaderMap to request instance.
112126
func SetHeaderMap(req *http.Request, headerMap map[string]string) {
113127
for k, v := range headerMap {
114128
req.Header.Set(k, v)

netutil/httpreq/util_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,18 @@ func TestAddHeaders(t *testing.T) {
3636
httpreq.AddHeaders(req, http.Header{
3737
"key0": []string{"val0"},
3838
})
39-
4039
assert.Eq(t, "val0", req.Header.Get("key0"))
40+
41+
httpreq.AddHeaderMap(req, map[string]string{
42+
"key1": "val1",
43+
})
44+
assert.Eq(t, "val1", req.Header.Get("key1"))
45+
46+
// IsValidMethod
47+
assert.NotEmpty(t, httpreq.ValidMethods)
48+
assert.True(t, httpreq.IsValidMethod("get"))
49+
assert.True(t, httpreq.IsValidMethod("GET"))
50+
assert.False(t, httpreq.IsValidMethod("get1"))
4151
}
4252

4353
func TestHeaderToStringMap(t *testing.T) {
@@ -67,6 +77,12 @@ func TestToQueryValues(t *testing.T) {
6777
"field2": {"value2"},
6878
})
6979
assert.StrContains(t, vs.Encode(), "field1=234")
80+
81+
reqURL, err := url.Parse("abc.dev")
82+
assert.NoErr(t, err)
83+
err = httpreq.AppendQueryToURL(reqURL, vs)
84+
assert.NoErr(t, err)
85+
assert.Eq(t, "field1=234&field2=value2", reqURL.RawQuery)
7086
}
7187

7288
func TestRequestToString(t *testing.T) {

0 commit comments

Comments
 (0)