-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresponse_meta_test.go
More file actions
76 lines (69 loc) · 1.7 KB
/
response_meta_test.go
File metadata and controls
76 lines (69 loc) · 1.7 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
package httpsuite
import (
"encoding/json"
"strings"
"testing"
)
func TestMetaSerialization(t *testing.T) {
t.Parallel()
pageResponse := Response[[]string]{
Data: []string{"a"},
Meta: NewPageMeta(1, 10, 15),
}
pageBody, err := json.Marshal(pageResponse)
if err != nil {
t.Fatalf("marshal page response: %v", err)
}
if !json.Valid(pageBody) {
t.Fatalf("expected valid page response json")
}
cursorResponse := Response[[]string]{
Data: []string{"a"},
Meta: NewCursorMeta("next", "", true, false),
}
cursorBody, err := json.Marshal(cursorResponse)
if err != nil {
t.Fatalf("marshal cursor response: %v", err)
}
if !json.Valid(cursorBody) {
t.Fatalf("expected valid cursor response json")
}
}
func TestResponseSerializationPreservesMeaningfulZeroValues(t *testing.T) {
t.Parallel()
tests := []struct {
name string
response any
wantContains []string
}{
{
name: "zero data value",
response: Response[int]{Data: 0},
wantContains: []string{`"data":0`},
},
{
name: "false bool data value",
response: Response[bool]{Data: false},
wantContains: []string{`"data":false`},
},
{
name: "cursor false flags",
response: Response[[]string]{Data: []string{"a"}, Meta: NewCursorMeta("", "", false, false)},
wantContains: []string{`"has_next":false`, `"has_prev":false`},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
body, err := json.Marshal(tt.response)
if err != nil {
t.Fatalf("marshal response: %v", err)
}
for _, want := range tt.wantContains {
if !strings.Contains(string(body), want) {
t.Fatalf("expected %q in %q", want, string(body))
}
}
})
}
}