-
Notifications
You must be signed in to change notification settings - Fork 0
/
res_test.go
64 lines (62 loc) · 1.16 KB
/
res_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 res
import (
"bytes"
"testing"
)
func TestUnmarshal(t *testing.T) {
type args struct {
bytes []byte
}
tests := []struct {
name string
args args
wantCode int
wantErr bool
}{
{"invalid data",
args{nil},
0,
true},
{"invalid json",
args{
[]byte(`{
"code":200,
"request_id":"bobbook/2Mch7LMzhj-000001",
"mess`),
},
0,
true},
{"ok",
args{
[]byte(`{
"code":200,
"request_id":"bobbook/2Mch7LMzhj-000001",
"message":"session created",
"data":{
"token":"blah"
}
}`),
},
200,
false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var gotToken string
var gotKV = []KV{{Key: "token", Value: &gotToken}}
got, err := Unmarshal(bytes.NewReader(tt.args.bytes), gotKV...)
if (err != nil) != tt.wantErr {
t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != nil && tt.wantCode != got.HTTPStatusCode {
t.Errorf("Unmarshal() code = %v, want %v", got.HTTPStatusCode, tt.wantCode)
}
for _, kv := range gotKV {
if kv.Value == "" {
t.Error("Unmarshal() kv is empty")
}
}
})
}
}