Skip to content

Commit

Permalink
feat: initialize client_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
ViolaPioggia committed May 22, 2024
1 parent cc62cee commit ac162ef
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 38 deletions.
11 changes: 9 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net/http"
"net/url"
"regexp"
"strings"
"sync"
)

Expand Down Expand Up @@ -114,7 +115,14 @@ func (c *Client) SetQueryParamsFromValues(params url.Values) *Client {
}

func (c *Client) SetQueryString(query string) *Client {
// todo: parse query string
str := strings.Split(query, "&")
for _, v := range str {
kv := strings.Split(v, "=")
if len(kv) == 2 {
c.QueryParam.Set(kv[0], kv[1])
}

}
return c
}

Expand Down Expand Up @@ -229,7 +237,6 @@ func (c *Client) R() *Request {
r := &Request{
QueryParam: url.Values{},
Header: http.Header{},
Cookies: make([]*http.Cookie, 0),
PathParams: map[string]string{},
RawRequest: &protocol.Request{},

Expand Down
20 changes: 20 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package easy_http

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestSetQueryParam(t *testing.T) {
c := MustNew()
c.SetQueryParam("test1", "test1")
c.SetQueryParams(map[string]string{"test2": "test2", "test3": "test3"})
c.SetQueryParamsFromValues(map[string][]string{"test4": {"test41", "test42"}})
c.SetQueryString("test5=test5")

assert.Equal(t, "test1", c.QueryParam.Get("test1"))
assert.Equal(t, "test2", c.QueryParam.Get("test2"))
assert.Equal(t, "test3", c.QueryParam.Get("test3"))
assert.Equal(t, []string{"test41", "test42"}, c.QueryParam["test4"])
assert.Equal(t, "test5", c.QueryParam.Get("test5"))
}
1 change: 1 addition & 0 deletions easy_http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package easy_http
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@ module github.com/hertz-contrib/easy_http

go 1.19

require github.com/cloudwego/hertz v0.8.1
require (
github.com/cloudwego/hertz v0.8.1
github.com/stretchr/testify v1.8.1
)

require (
github.com/bytedance/go-tagexpr/v2 v2.9.2 // indirect
github.com/bytedance/gopkg v0.0.0-20220413063733-65bf48ffb3a7 // indirect
github.com/bytedance/sonic v1.8.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/cloudwego/netpoll v0.5.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/golang/protobuf v1.5.0 // indirect
github.com/henrylee2cn/ameda v1.4.10 // indirect
github.com/henrylee2cn/goutil v0.0.0-20210127050712-89660552f6f8 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/nyaruka/phonenumbers v1.0.55 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tidwall/gjson v1.14.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
75 changes: 40 additions & 35 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type Request struct {
QueryParam url.Values
FormData url.Values
Header http.Header
Cookies []*http.Cookie
PathParams map[string]string
FileData map[string]string
BodyParams interface{}
Expand All @@ -45,8 +44,8 @@ type Request struct {
RequestOptions []config.RequestOption
Result interface{}
Error interface{}
isMultiPart bool
multipartFiles []*File
isMultiPart bool
multipartFiles []*File
}

const (
Expand All @@ -71,11 +70,13 @@ const (
// MethodOptions HTTP method
MethodOptions = "OPTIONS"
)

type File struct {
Name string
ParamName string
io.Reader
}

func (r *Request) SetQueryParam(param, value string) *Request {
r.QueryParam.Set(param, value)
return r
Expand Down Expand Up @@ -103,17 +104,17 @@ func (r *Request) SetQueryString(query string) *Request {
}
}
} else {
fmt.Printf("%v",err)
fmt.Printf("%v", err)
}
return r
}
func (r * Request) AddQueryParam(params,value string)*Request{
r.QueryParam.Add(params,value)
func (r *Request) AddQueryParam(params, value string) *Request {
r.QueryParam.Add(params, value)
return r
}
func (r *Request) AddQueryParams(params map[string]string)*Request{
for k,v :=range params{
r.AddQueryParam(k,v)
func (r *Request) AddQueryParams(params map[string]string) *Request {
for k, v := range params {
r.AddQueryParam(k, v)
}
return r
}
Expand Down Expand Up @@ -146,50 +147,54 @@ func (r *Request) SetHeaderMultiValues(headers map[string][]string) *Request {
}
return r
}
func (r * Request) AddHeader(header , value string)*Request{
r.Header.Add(header,value)
func (r *Request) AddHeader(header, value string) *Request {
r.Header.Add(header, value)
return r
}
func (r *Request) AddHeaders(headers map[string]string)*Request{
for k , v :=range headers{
r.AddHeader(k,v)
func (r *Request) AddHeaders(headers map[string]string) *Request {
for k, v := range headers {
r.AddHeader(k, v)
}
return r
}
func (r *Request)AddHeaderMultiValues(headers map[string][]string)*Request{
for key ,value := range headers{
r.AddHeader(key,strings.Join(value,", "))
}
func (r *Request) AddHeaderMultiValues(headers map[string][]string) *Request {
for key, value := range headers {
r.AddHeader(key, strings.Join(value, ", "))
}
return r
}

// [] SetContentType(contentType string)
// [] SetJSONContentType() (可添加一些常用的 content-type)
func (r *Request) SetCookie(hc *http.Cookie) *Request {
r.Cookies = append(r.Cookies, hc)
r.RawRequest.SetCookie(hc.Name, hc.Value)
return r
}
func (r *Request) SetCookies(rs []*http.Cookie) *Request {
r.Cookies = append(r.Cookies, rs...)
for _, c := range rs {
r.RawRequest.SetCookie(c.Name, c.Value)
}
return r
}

// [] SetJSONBody(body interface{}) (自动注入 json content-type,参数可以是 strcut、map、[]byte、string 等)
// [] SetUrlEncodeBody(body url.Value) (自动注入 urlencode content-type)
func (r *Request) SetBody(body interface{}) *Request {
r.BodyParams= body
r.BodyParams = body
return r
}
func (r *Request) SetFormData(data map[string]string) *Request {
for k ,v :=range data{
r.FormData.Set(k,v)
for k, v := range data {
r.FormData.Set(k, v)
}
return r
}
func (r *Request) SetFormDataFromValues(data url.Values) *Request {
for key ,value := range data{
for _,v :=range value{
r.FormData.Add(key,v)
for key, value := range data {
for _, v := range value {
r.FormData.Add(key, v)
}
}
}
return r
}
func (r *Request) SetFiles(files map[string]string) *Request {
Expand All @@ -212,33 +217,34 @@ func (r *Request) SetFileReader(param, fileName string, reader io.Reader) *Reque
func (r *Request) SetResult(res interface{}) *Request {
if res != nil {
vv := reflect.ValueOf(res)
if vv.Kind() == reflect.Ptr{
if vv.Kind() == reflect.Ptr {
r.Result = res
}else {
} else {
r.Result = reflect.New(vv.Type()).Interface()
}
}
return r
}

// [] WithContext(ctx)
// [] WithDC(dc)
// [] WithCluster(cluster)
// [] WithEnv(env)
// [] WIthCallTimeout(t)
func (r * Request)WithContext(ctx context.Context)*Request{
func (r *Request) WithContext(ctx context.Context) *Request {
r.Ctx = ctx
return r
}
func (r * Request)WithDC()*Request{
func (r *Request) WithDC() *Request {
return r
}
func (r * Request)WithCluster()*Request{
func (r *Request) WithCluster() *Request {
return r
}
func (r * Request)WithEnv()*Request{
func (r *Request) WithEnv() *Request {
return r
}
func (r * Request)WIthCallTimeout()*Request{
func (r *Request) WIthCallTimeout() *Request {
return r
}
func (r *Request) Get(url string) (*Response, error) {
Expand Down Expand Up @@ -286,4 +292,3 @@ func (r *Request) Execute(method, url string) (*Response, error) {
res, err = r.client.execute(r)
return res, err
}

0 comments on commit ac162ef

Please sign in to comment.