-
Notifications
You must be signed in to change notification settings - Fork 11
/
http_test.go
92 lines (73 loc) · 2.65 KB
/
http_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
package courier
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestClient_TelemetryHandler(t *testing.T) {
tests := []struct {
name string
status int
body string
opts func(*testing.T) []ClientOption
}{
{
name: "single connection mode",
status: http.StatusOK,
opts: func(t *testing.T) []ClientOption { return nil },
body: fmt.Sprintf(`{"multi":false,"clients":[{"addresses":[{"host":"%s","port":%d}],"client_id":"clientID","username":"","resume_subs":false,"clean_session":false,"auto_reconnect":true,"connected":true}],"subscriptions":{"$share/test/test-topic":0}}
`, testBrokerAddress.Host, testBrokerAddress.Port),
},
{
name: "multi connection mode",
status: http.StatusOK,
opts: func(t *testing.T) []ClientOption {
ch := make(chan []TCPAddress, 1)
dCh := make(chan struct{})
mr := newMockResolver(t)
mr.On("UpdateChan").Return(ch)
mr.On("Done").Return(dCh)
go func() {
ch <- []TCPAddress{testBrokerAddress, testBrokerAddress}
<-time.After(2 * time.Second)
close(ch)
dCh <- struct{}{}
}()
return []ClientOption{
WithResolver(mr),
UseMultiConnectionMode,
}
},
body: fmt.Sprintf(`{"multi":true,"clients":[{"addresses":[{"host":"%s","port":%d}],"client_id":"clientID-0-1","username":"","resume_subs":false,"clean_session":false,"auto_reconnect":true,"connected":true,"subscriptions":["$share/test/test-topic"]},{"addresses":[{"host":"%s","port":%d}],"client_id":"clientID-1-1","username":"","resume_subs":false,"clean_session":false,"auto_reconnect":true,"connected":true,"subscriptions":["$share/test/test-topic"]}],"subscriptions":{"$share/test/test-topic":0}}
`, testBrokerAddress.Host, testBrokerAddress.Port, testBrokerAddress.Host, testBrokerAddress.Port),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
eOpts := tt.opts(t)
c, err := NewClient(append(defOpts, eOpts...)...)
assert.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
_ = c.Run(ctx)
}()
WaitForConnection(c, 2*time.Second, 100*time.Millisecond)
assert.NoError(t, c.Subscribe(context.TODO(), "$share/test/test-topic",
func(ctx context.Context, ps PubSub, msg *Message) {
// do nothing
}))
h := c.InfoHandler()
assert.True(t, WaitForConnection(c, 2*time.Second, 100*time.Millisecond))
req := httptest.NewRequest(http.MethodGet, "/telemetry", nil)
rr := httptest.NewRecorder()
h.ServeHTTP(rr, req)
assert.Equal(t, tt.status, rr.Code)
assert.Equal(t, tt.body, rr.Body.String())
})
}
}