forked from dunglas/mercure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub_test.go
204 lines (163 loc) · 4.36 KB
/
hub_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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package mercure
import (
"errors"
"net/http/httptest"
"os"
"os/exec"
"sync"
"testing"
"github.com/golang-jwt/jwt/v4"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)
const (
testAddr = "127.0.0.1:4242"
testMetricsAddr = "127.0.0.1:4243"
)
func TestNewHub(t *testing.T) {
h := createDummy()
assert.IsType(t, &viper.Viper{}, h.config)
}
func TestNewHubWithConfig(t *testing.T) {
h, err := NewHub(
WithPublisherJWT([]byte("foo"), jwt.SigningMethodHS256.Name),
WithSubscriberJWT([]byte("bar"), jwt.SigningMethodHS256.Name),
)
require.NotNil(t, h)
require.Nil(t, err)
}
func TestNewHubValidationError(t *testing.T) {
assert.Panics(t, func() {
NewHubFromViper(viper.New())
})
}
func TestNewHubTransportValidationError(t *testing.T) {
v := viper.New()
v.Set("publisher_jwt_key", "foo")
v.Set("jwt_key", "bar")
v.Set("transport_url", "foo://")
assert.Panics(t, func() {
NewHubFromViper(viper.New())
})
}
func TestStartCrash(t *testing.T) {
if os.Getenv("BE_START_CRASH") == "1" {
Start()
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestStartCrash") //nolint:gosec
cmd.Env = append(os.Environ(), "BE_START_CRASH=1")
err := cmd.Run()
var e *exec.ExitError
require.True(t, errors.As(err, &e))
assert.False(t, e.Success())
}
func TestStop(t *testing.T) {
numberOfSubscribers := 2
hub := createAnonymousDummy()
go func() {
s := hub.transport.(*LocalTransport)
var ready bool
for !ready {
s.RLock()
ready = s.subscribers.Len() == numberOfSubscribers
s.RUnlock()
}
hub.transport.Dispatch(&Update{
Topics: []string{"http://example.com/foo"},
Event: Event{Data: "Hello World"},
})
hub.Stop()
}()
var wg sync.WaitGroup
wg.Add(numberOfSubscribers)
for i := 0; i < numberOfSubscribers; i++ {
go func() {
defer wg.Done()
req := httptest.NewRequest("GET", defaultHubURL+"?topic=http://example.com/foo", nil)
w := httptest.NewRecorder()
hub.SubscribeHandler(w, req)
r := w.Result()
r.Body.Close()
assert.Equal(t, 200, r.StatusCode)
}()
}
wg.Wait()
}
func TestWithProtocolVersionCompatibility(t *testing.T) {
op := &opt{}
assert.False(t, op.isBackwardCompatiblyEnabledWith(7))
o := WithProtocolVersionCompatibility(7)
require.Nil(t, o(op))
assert.Equal(t, 7, op.protocolVersionCompatibility)
assert.True(t, op.isBackwardCompatiblyEnabledWith(7))
assert.True(t, op.isBackwardCompatiblyEnabledWith(8))
assert.False(t, op.isBackwardCompatiblyEnabledWith(6))
}
func TestInvalidWithProtocolVersionCompatibility(t *testing.T) {
op := &opt{}
o := WithProtocolVersionCompatibility(6)
require.NotNil(t, o(op))
}
func createDummy(options ...Option) *Hub {
tss, _ := NewTopicSelectorStoreLRU(0, 0)
options = append(
[]Option{
WithPublisherJWT([]byte("publisher"), jwt.SigningMethodHS256.Name),
WithSubscriberJWT([]byte("subscriber"), jwt.SigningMethodHS256.Name),
WithLogger(zap.NewNop()),
WithTopicSelectorStore(tss),
},
options...,
)
h, _ := NewHub(options...)
h.config = viper.New()
h.config.Set("addr", testAddr)
h.config.Set("metrics_addr", testMetricsAddr)
return h
}
func createAnonymousDummy(options ...Option) *Hub {
options = append(
[]Option{WithAnonymous()},
options...,
)
return createDummy(options...)
}
func createDummyAuthorizedJWT(h *Hub, r role, topics []string) string {
token := jwt.New(jwt.SigningMethodHS256)
var key []byte
switch r {
case rolePublisher:
token.Claims = &claims{Mercure: mercureClaim{Publish: topics}, RegisteredClaims: jwt.RegisteredClaims{}}
key = h.publisherJWT.key
case roleSubscriber:
var payload struct {
Foo string `json:"foo"`
}
payload.Foo = "bar"
token.Claims = &claims{
Mercure: mercureClaim{
Subscribe: topics,
Payload: payload,
},
RegisteredClaims: jwt.RegisteredClaims{},
}
key = h.subscriberJWT.key
}
tokenString, _ := token.SignedString(key)
return tokenString
}
func createDummyUnauthorizedJWT() string {
token := jwt.New(jwt.SigningMethodHS256)
tokenString, _ := token.SignedString([]byte("unauthorized"))
return tokenString
}
func createDummyNoneSignedJWT() string {
token := jwt.New(jwt.SigningMethodNone)
// The generated token must have more than 41 chars
token.Claims = jwt.RegisteredClaims{Subject: "me"}
tokenString, _ := token.SignedString(jwt.UnsafeAllowNoneSignatureType)
return tokenString
}