-
Notifications
You must be signed in to change notification settings - Fork 117
/
page_token_test.go
84 lines (79 loc) · 1.75 KB
/
page_token_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package query
import (
"testing"
)
func TestDecodePageToken(t *testing.T) {
tcases := []struct {
PageToken string
Offset int32
Limit int32
ExpectedError string
}{
{
PageToken: "asd", //invalid
ExpectedError: "Invalid page token \"illegal base64 data at input byte 0\".",
},
{
PageToken: "MTI=", //12
ExpectedError: "Malformed page token.",
},
{
PageToken: "YTpi", //a:b
ExpectedError: "Page token validation failed.",
},
{
PageToken: "MTI6Yg==", //12:b
ExpectedError: "Page token validation failed.",
},
{
PageToken: "YToxMg==", //a:12
ExpectedError: "Page token validation failed.",
},
{
PageToken: "MTI6MzQ=", //12:34
Limit: 34,
Offset: 12,
},
{
PageToken: "MzQ6MTI=", //34:12
Limit: 12,
Offset: 34,
},
}
for n, tc := range tcases {
offset, limit, err := DecodePageToken(tc.PageToken)
if (err != nil && tc.ExpectedError != err.Error()) || (err == nil && tc.ExpectedError != "") {
t.Fatalf("tc %d: invalid error %q, expected %q", n, err, tc.ExpectedError)
}
if limit != tc.Limit {
t.Fatalf("tc %d: invalid limit %d, expected %d", n, limit, tc.Limit)
}
if offset != tc.Offset {
t.Fatalf("tc %d: invalid offset %d, expected %d", n, offset, tc.Offset)
}
}
}
func TestEncodePageToken(t *testing.T) {
tcases := []struct {
Offset int32
Limit int32
PageToken string
}{
{
Limit: 34,
Offset: 12,
PageToken: "MTI6MzQ=",
},
{
Limit: 12,
Offset: 34,
PageToken: "MzQ6MTI=",
},
}
for n, tc := range tcases {
ptoken := EncodePageToken(tc.Offset, tc.Limit)
if ptoken != tc.PageToken {
t.Fatalf("tc %d: invalid page token %q, expected %q", n, ptoken, tc.PageToken)
}
}
}