File tree Expand file tree Collapse file tree 2 files changed +45
-1
lines changed Expand file tree Collapse file tree 2 files changed +45
-1
lines changed Original file line number Diff line number Diff line change @@ -265,7 +265,7 @@ func (r *Response) wrapCopyReadCloser() {
265
265
f : func (b * bytes.Buffer ) {
266
266
r .bodyBytes = append ([]byte {}, b .Bytes ()... )
267
267
closeq (r .Body )
268
- r .Body = & nopReadCloser { r : bytes .NewReader (r .bodyBytes )}
268
+ r .Body = io . NopCloser ( bytes .NewReader (r .bodyBytes ))
269
269
releaseBuffer (b )
270
270
},
271
271
}
Original file line number Diff line number Diff line change
1
+ package resty
2
+
3
+ import (
4
+ "bytes"
5
+ "io"
6
+ "net/http"
7
+ "net/http/httptest"
8
+ "testing"
9
+ )
10
+
11
+ func TestDecodeJSONWhenResponseBodyIsNull (t * testing.T ) {
12
+ r := & Response {
13
+ Body : io .NopCloser (bytes .NewReader ([]byte ("null" ))),
14
+ }
15
+ r .wrapCopyReadCloser ()
16
+ err := r .readAll ()
17
+ assertNil (t , err )
18
+
19
+ var result map [int ]int
20
+ err = decodeJSON (r .Body , & result )
21
+ assertNil (t , err )
22
+ assertNil (t , result )
23
+ }
24
+
25
+ func TestGetMethodWhenResponseIsNull (t * testing.T ) {
26
+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
27
+ w .Write ([]byte ("null" ))
28
+ }))
29
+
30
+ client := New ().SetRetryCount (3 ).EnableGenerateCurlCmd ()
31
+
32
+ var x any
33
+ resp , err := client .R ().SetBody ("{}" ).
34
+ SetHeader ("Content-Type" , "application/json; charset=utf-8" ).
35
+ SetForceResponseContentType ("application/json" ).
36
+ SetAllowMethodGetPayload (true ).
37
+ SetResponseBodyUnlimitedReads (true ).
38
+ SetResult (& x ).
39
+ Get (server .URL + "/test" )
40
+
41
+ assertNil (t , err )
42
+ assertEqual (t , "null" , resp .String ())
43
+ assertEqual (t , nil , x )
44
+ }
You can’t perform that action at this time.
0 commit comments