Skip to content

Commit c9740e0

Browse files
committed
#11 Request.SetQueryString method is done and test cases.
1 parent a2cbc36 commit c9740e0

File tree

3 files changed

+98
-32
lines changed

3 files changed

+98
-32
lines changed

client.go

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,7 @@ func (c *Client) SetContentLength(l bool) *Client {
354354
// resty.SetError(Error{})
355355
//
356356
func (c *Client) SetError(err interface{}) *Client {
357-
t := reflect.TypeOf(err)
358-
if t.Kind() == reflect.Ptr {
359-
c.Error = t.Elem()
360-
} else {
361-
c.Error = t
362-
}
357+
c.Error = getType(err)
363358
return c
364359
}
365360

@@ -463,7 +458,7 @@ func (c *Client) SetTimeout(timeout time.Duration) *Client {
463458
c.transport.Dial = func(network, addr string) (net.Conn, error) {
464459
conn, err := net.DialTimeout(network, addr, timeout)
465460
if err != nil {
466-
c.Log.Printf("Error: %v", err)
461+
c.Log.Printf("ERROR [%v]", err)
467462
return nil, err
468463
}
469464
conn.SetDeadline(time.Now().Add(timeout))
@@ -484,7 +479,7 @@ func (c *Client) SetProxy(proxyURL string) *Client {
484479
if pURL, err := url.Parse(proxyURL); err == nil {
485480
c.transport.Proxy = http.ProxyURL(pURL)
486481
} else {
487-
c.Log.Printf("ERROR: %v", err)
482+
c.Log.Printf("ERROR [%v]", err)
488483
}
489484

490485
return c
@@ -643,6 +638,24 @@ func (r *Request) SetQueryParams(params map[string]string) *Request {
643638
return r
644639
}
645640

641+
// SetQueryString method provides ability to use string as an input to set URL query string for the request.
642+
//
643+
// Using String as an input
644+
// resty.R().
645+
// SetQueryString("productId=232&template=fresh-sample&cat=resty&source=google&kw=buy a lot more")
646+
//
647+
func (r *Request) SetQueryString(query string) *Request {
648+
values, err := url.ParseQuery(strings.TrimSpace(query))
649+
if err == nil {
650+
for p, _ := range values {
651+
r.QueryParam.Add(p, values.Get(p))
652+
}
653+
} else {
654+
r.client.Log.Printf("ERROR [%v]", err)
655+
}
656+
return r
657+
}
658+
646659
// SetFormData method sets Form parameters and its values in the current request.
647660
// It's applicable only HTTP method `POST` and `PUT` and requets content type would be set as
648661
// `application/x-www-form-urlencoded`.
@@ -934,12 +947,7 @@ func IsStringEmpty(str string) bool {
934947
// DetectContentType method is used to figure out `Request.Body` content type for request header
935948
func DetectContentType(body interface{}) string {
936949
contentType := plainTextType
937-
kind := reflect.ValueOf(body).Kind()
938-
if kind == reflect.Ptr {
939-
kind = reflect.TypeOf(body).Elem().Kind()
940-
}
941-
942-
switch kind {
950+
switch getBaseKind(body) {
943951
case reflect.Struct, reflect.Map:
944952
contentType = jsonContentType
945953
case reflect.String:
@@ -973,14 +981,7 @@ func Unmarshal(ct string, b []byte, d interface{}) (err error) {
973981
}
974982

975983
func getLogger(w io.Writer) *log.Logger {
976-
var l *log.Logger
977-
if w == nil {
978-
l = log.New(os.Stderr, "RESTY ", log.LstdFlags)
979-
} else {
980-
l = log.New(w, "RESTY ", log.LstdFlags)
981-
}
982-
983-
return l
984+
return log.New(w, "RESTY ", log.LstdFlags)
984985
}
985986

986987
func addFile(w *multipart.Writer, fieldName, path string) error {
@@ -1064,14 +1065,25 @@ func getResponseBodyString(res *Response) string {
10641065
}
10651066

10661067
func getPointer(v interface{}) interface{} {
1067-
rv := reflect.TypeOf(v)
1068-
if rv.Kind() != reflect.Ptr {
1069-
return reflect.New(rv).Interface()
1068+
vv := reflect.ValueOf(v)
1069+
if vv.Kind() == reflect.Ptr {
1070+
return v
10701071
}
1071-
1072-
return v
1072+
return reflect.New(vv.Type()).Interface()
10731073
}
10741074

10751075
func isPayloadSupported(m string) bool {
10761076
return (m == POST || m == PUT || m == DELETE || m == PATCH)
10771077
}
1078+
1079+
func getBaseKind(v interface{}) reflect.Kind {
1080+
return getType(v).Kind()
1081+
}
1082+
1083+
func getType(v interface{}) reflect.Type {
1084+
vv := reflect.ValueOf(v)
1085+
if vv.Kind() == reflect.Ptr {
1086+
return vv.Elem().Type()
1087+
}
1088+
return vv.Type()
1089+
}

middleware.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,7 @@ func parseRequestBody(c *Client, r *Request) (err error) {
136136
}
137137

138138
var bodyBytes []byte
139-
kind := reflect.ValueOf(r.Body).Kind()
140-
if kind == reflect.Ptr {
141-
kind = reflect.TypeOf(r.Body).Elem().Kind()
142-
}
143-
139+
kind := getBaseKind(r.Body)
144140
if IsJSONType(contentType) && (kind == reflect.Struct || kind == reflect.Map) {
145141
bodyBytes, err = json.Marshal(r.Body)
146142
} else if IsXMLType(contentType) && (kind == reflect.Struct) {

resty_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,28 @@ func TestMultiPartUploadFile(t *testing.T) {
492492
assertEqual(t, http.StatusOK, resp.StatusCode())
493493
}
494494

495+
func TestMultiPartUploadFileError(t *testing.T) {
496+
ts := createFormPostServer(t)
497+
defer ts.Close()
498+
499+
pwd, _ := os.Getwd()
500+
basePath := pwd + "/test-data"
501+
502+
c := dc()
503+
c.SetFormData(map[string]string{"zip_code": "00001", "city": "Los Angeles"})
504+
505+
resp, err := c.R().
506+
SetFile("profile_img", basePath+"/test-img-not-exists.png").
507+
Post(ts.URL + "/upload")
508+
509+
if err == nil {
510+
t.Errorf("Expected [%v], got [%v]", nil, err)
511+
}
512+
if resp != nil {
513+
t.Errorf("Expected [%v], got [%v]", nil, resp)
514+
}
515+
}
516+
495517
func TestMultiPartUploadFiles(t *testing.T) {
496518
ts := createFormPostServer(t)
497519
defer ts.Close()
@@ -747,6 +769,14 @@ func TestClientTimeout(t *testing.T) {
747769
assertEqual(t, true, strings.Contains(err.Error(), "i/o timeout"))
748770
}
749771

772+
func TestClientTimeoutInternalError(t *testing.T) {
773+
c := dc()
774+
c.SetHTTPMode()
775+
c.SetTimeout(time.Duration(time.Second * 1))
776+
777+
c.R().Get("http://localhost:9000/set-timeout-test")
778+
}
779+
750780
func TestHeadMethod(t *testing.T) {
751781
ts := createGetServer(t)
752782
defer ts.Close()
@@ -843,6 +873,34 @@ func TestDetectContentTypeForPointer(t *testing.T) {
843873
logResponse(t, resp)
844874
}
845875

876+
func TestSetQueryStringTypical(t *testing.T) {
877+
ts := createGetServer(t)
878+
defer ts.Close()
879+
880+
resp, err := dclr().
881+
SetQueryString("productId=232&template=fresh-sample&cat=resty&source=google&kw=buy a lot more").
882+
Get(ts.URL)
883+
884+
assertError(t, err)
885+
assertEqual(t, http.StatusOK, resp.StatusCode())
886+
assertEqual(t, "200 OK", resp.Status())
887+
assertEqual(t, "TestGet: text response", resp.String())
888+
}
889+
890+
func TestSetQueryStringTypicalError(t *testing.T) {
891+
ts := createGetServer(t)
892+
defer ts.Close()
893+
894+
resp, err := dclr().
895+
SetQueryString("&%%amp;").
896+
Get(ts.URL)
897+
898+
assertError(t, err)
899+
assertEqual(t, http.StatusOK, resp.StatusCode())
900+
assertEqual(t, "200 OK", resp.Status())
901+
assertEqual(t, "TestGet: text response", resp.String())
902+
}
903+
846904
func TestClientOptions(t *testing.T) {
847905
SetHTTPMode().SetContentLength(true)
848906
assertEqual(t, Mode(), "http")

0 commit comments

Comments
 (0)