Repository: go-resty/resty (go)

Issue:
SetResult Add nil Check
SetResult Add nil Check, avoid panic

Pull request description:
feat: add nil check to SetResult (#503)
fixes #503.

Simply adds a `nil` check to `SetResult` to ensure that it can be used fluently. Also adds a simple unit test to ensure that the current panic will not happen any longer.

Patch:
diff --git a/request.go b/request.go
index e6043d7..e2f9aed 100644
--- a/request.go
+++ b/request.go
@@ -303,7 +303,9 @@ func (r *Request) SetBody(body interface{}) *Request {
 // Accessing a result value from response instance.
 //		response.Result().(*AuthToken)
 func (r *Request) SetResult(res interface{}) *Request {
-	r.Result = getPointer(res)
+	if res != nil {
+		r.Result = getPointer(res)
+	}
 	return r
 }
 


Test patch:
diff --git a/request_test.go b/request_test.go
index 33d524e..06aef36 100644
--- a/request_test.go
+++ b/request_test.go
@@ -1891,3 +1891,13 @@ func TestPostBodyError(t *testing.T) {
 	assertEqual(t, "read error", err.Error())
 	assertNil(t, resp)
 }
+
+func TestSetResultMustNotPanicOnNil(t *testing.T) {
+	defer func() {
+		if r := recover(); r != nil {
+			t.Errorf("must not panic")
+		}
+	}()
+	dc().R().SetResult(nil)
+}
+