-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_counts_request_test.go
170 lines (136 loc) · 5.44 KB
/
message_counts_request_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package pubnub
import (
"fmt"
"testing"
h "github.com/pubnub/go/v5/tests/helpers"
"github.com/stretchr/testify/assert"
)
func AssertSuccessMessageCountsGet(t *testing.T, expectedString string, channels []string, timetoken int64, channelsTimetoken []int64) {
assert := assert.New(t)
opts := &messageCountsOpts{
Channels: channels,
Timetoken: timetoken,
ChannelsTimetoken: channelsTimetoken,
pubnub: pubnub,
}
path, err := opts.buildPath()
assert.Nil(err)
h.AssertPathsEqual(t,
fmt.Sprintf("/v3/history/sub-key/sub_key/message-counts/%s", expectedString),
path, []int{})
body, err := opts.buildBody()
assert.Nil(err)
assert.Empty(body)
}
func TestMessageCountsPath(t *testing.T) {
channels := []string{"test1", "test2"}
channelsTimetoken := []int64{15499825804610610, 15499925804610615}
AssertSuccessMessageCountsGet(t, "test1,test2", channels, 15499825804610610, channelsTimetoken)
}
func TestMessageCountsQuery(t *testing.T) {
channels := []string{"test1", "test2"}
channelsTimetoken := []int64{15499825804610610, 15499925804610615}
AssertSuccessMessageCountsGetQuery(t, "", "15499825804610610,15499925804610615", channels, 15499825804610610, channelsTimetoken)
}
func TestMessageCountsQuery2(t *testing.T) {
channels := []string{"test1", "test2"}
channelsTimetoken := []int64{}
AssertSuccessMessageCountsGetQuery(t, "", "", channels, 15499825804610610, channelsTimetoken)
}
func TestMessageCountsQuery3(t *testing.T) {
channels := []string{"test1", "test2"}
channelsTimetoken := []int64{15499825804610610, 15499925804610615}
AssertSuccessMessageCountsGetQuery(t, "", "15499825804610610,15499925804610615", channels, 0, channelsTimetoken)
}
func AssertSuccessMessageCountsGetQuery(t *testing.T, expectedString1 string, expectedString2 string, channels []string, timetoken int64, channelsTimetoken []int64) {
assert := assert.New(t)
queryParam := map[string]string{
"q1": "v1",
"q2": "v2",
}
opts := &messageCountsOpts{
Channels: channels,
Timetoken: timetoken,
ChannelsTimetoken: channelsTimetoken,
pubnub: pubnub,
QueryParam: queryParam,
}
u, err := opts.buildQuery()
assert.Nil(err)
assert.Equal("v1", u.Get("q1"))
assert.Equal("v2", u.Get("q2"))
assert.Equal(expectedString1, u.Get("timetoken"))
assert.Equal(expectedString2, u.Get("channelsTimetoken"))
}
func AssertNewMessageCountsBuilder(t *testing.T, testQueryParam bool, testContext bool, expectedString string, expectedString1 string, expectedString2 string, channels []string, timetoken int64, channelsTimetoken []int64) {
assert := assert.New(t)
queryParam := map[string]string{
"q1": "v1",
"q2": "v2",
}
o := newMessageCountsBuilder(pubnub)
if testContext {
o = newMessageCountsBuilderWithContext(pubnub, backgroundContext)
}
o.Channels(channels)
o.Timetoken(timetoken)
o.ChannelsTimetoken(channelsTimetoken)
if testQueryParam {
o.QueryParam(queryParam)
}
path, err := o.opts.buildPath()
assert.Nil(err)
h.AssertPathsEqual(t,
fmt.Sprintf("/v3/history/sub-key/sub_key/message-counts/%s", expectedString),
path, []int{})
u, _ := o.opts.buildQuery()
if testQueryParam {
assert.Equal("v1", u.Get("q1"))
assert.Equal("v2", u.Get("q2"))
}
assert.Equal(expectedString1, u.Get("timetoken"))
assert.Equal(expectedString2, u.Get("channelsTimetoken"))
}
func TestMessageCountsBuilder(t *testing.T) {
channels := []string{"test1", "test2"}
channelsTimetoken := []int64{15499825804610610, 15499925804610615}
AssertNewMessageCountsBuilder(t, false, false, "test1,test2", "", "15499825804610610,15499925804610615", channels, 15499825804610610, channelsTimetoken)
}
func TestMessageCountsBuilderQP(t *testing.T) {
channels := []string{"test1", "test2"}
channelsTimetoken := []int64{15499825804610610, 15499925804610615}
AssertNewMessageCountsBuilder(t, true, false, "test1,test2", "", "15499825804610610,15499925804610615", channels, 15499825804610610, channelsTimetoken)
}
func TestMessageCountsBuilderContext(t *testing.T) {
channels := []string{"test1", "test2"}
channelsTimetoken := []int64{15499825804610610, 15499925804610615}
AssertNewMessageCountsBuilder(t, false, true, "test1,test2", "", "15499825804610610,15499925804610615", channels, 15499825804610610, channelsTimetoken)
}
func TestMessageCountsBuilderContextQP(t *testing.T) {
channels := []string{"test1", "test2"}
channelsTimetoken := []int64{15499825804610610, 15499925804610615}
AssertNewMessageCountsBuilder(t, true, true, "test1,test2", "", "15499825804610610,15499925804610615", channels, 15499825804610610, channelsTimetoken)
}
func TestMessageCountsResponseValueError(t *testing.T) {
assert := assert.New(t)
pn := NewPubNub(NewDemoConfig())
opts := &messageCountsOpts{
pubnub: pn,
}
jsonBytes := []byte(`s`)
_, _, err := newMessageCountsResponse(jsonBytes, opts, StatusResponse{})
assert.Equal("pubnub/parsing: Error unmarshalling response: {s}", err.Error())
}
//{"status": 200, "error": false, "error_message": "", "channels": {"my-channel1":1,"my-channel":2}}
func TestMessageCountsResponseValuePass(t *testing.T) {
assert := assert.New(t)
pn := NewPubNub(NewDemoConfig())
opts := &messageCountsOpts{
pubnub: pn,
}
jsonBytes := []byte(`{"status": 200, "error": false, "error_message": "", "channels": {"my-channel1":1,"my-channel":2}}`)
res, _, err := newMessageCountsResponse(jsonBytes, opts, StatusResponse{})
assert.Equal(2, res.Channels["my-channel"])
assert.Equal(1, res.Channels["my-channel1"])
assert.Nil(err)
}