-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_test.go
64 lines (61 loc) · 1.38 KB
/
response_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package httpclient
import (
"io"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDeserializeJSON(t *testing.T) {
type args struct {
resp *http.Response
target any
}
tests := []struct {
name string
args args
wantErrMessage string
want map[string]any
}{
{
name: "returns error from JSON marshalling",
args: args{
resp: &http.Response{
Body: io.NopCloser(strings.NewReader("{")),
},
target: &map[string]any{},
},
wantErrMessage: "unexpected end of JSON input",
},
{
name: "returns error when not passing a pointer",
args: args{
resp: &http.Response{
Body: io.NopCloser(strings.NewReader("{}")),
},
target: map[string]any{},
},
wantErrMessage: "pointer required, got map[string]interface {}",
},
{
name: "unmarshals the JSON payload to the passed pointer",
args: args{
resp: &http.Response{
Body: io.NopCloser(strings.NewReader(`{"hello": "world"}`)),
},
target: &map[string]any{},
},
want: map[string]any{"hello": "world"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := DeserializeJSON(tt.args.resp, tt.args.target)
if tt.wantErrMessage != "" {
assert.ErrorContains(t, err, tt.wantErrMessage)
} else {
assert.Equal(t, tt.want, *tt.args.target.(*map[string]any))
}
})
}
}