4
4
"bytes"
5
5
"io"
6
6
"net/http"
7
+ "strings"
7
8
"testing"
8
9
9
10
"github.com/stretchr/testify/assert"
@@ -17,16 +18,21 @@ func ResponseEqual(t *testing.T, actual, expected *http.Response) {
17
18
if expected .Header != nil {
18
19
assert .Equal (t , expected .Header , actual .Header )
19
20
}
20
- expectedBody , err := io .ReadAll (expected .Body )
21
- require .NoError (t , err )
22
- expected .Body .Close ()
23
- actualBody , err := io .ReadAll (actual .Body )
24
- require .NoError (t , err )
25
- actual .Body .Close ()
26
- // Restore the body stream in order to allow multiple assertions
27
- actual .Body = io .NopCloser (bytes .NewBuffer (actualBody ))
28
- assert .JSONEq (t , string (expectedBody ), string (actualBody ))
29
-
21
+ if expected .Body != nil {
22
+ expectedBody , err := io .ReadAll (expected .Body )
23
+ require .NoError (t , err )
24
+ expected .Body .Close ()
25
+ actualBody , err := io .ReadAll (actual .Body )
26
+ require .NoError (t , err )
27
+ actual .Body .Close ()
28
+ // Restore the body stream in order to allow multiple assertions
29
+ actual .Body = io .NopCloser (bytes .NewBuffer (actualBody ))
30
+ if strings .HasPrefix (actual .Header .Get ("Content-Type" ), "application/json" ) {
31
+ assert .JSONEq (t , string (expectedBody ), string (actualBody ))
32
+ } else {
33
+ assert .Equal (t , string (expectedBody ), string (actualBody ))
34
+ }
35
+ }
30
36
if expected .Request != nil {
31
37
assert .Equal (t , expected .Request .URL , actual .Request .URL )
32
38
assert .Equal (t , expected .Request .Method , actual .Request .Method )
0 commit comments