-
-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathhubtopic_deprecated_test.go
More file actions
72 lines (56 loc) 路 1.92 KB
/
Copy pathhubtopic_deprecated_test.go
File metadata and controls
72 lines (56 loc) 路 1.92 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
//go:build deprecated_topic && deprecated_claim
package mercure
import (
"testing"
"github.com/golang-jwt/jwt/v5"
)
func stringsToDeprecatedClaims(patterns []string) []matcherClaim {
matchers := stringsToDeprecatedMatchers(patterns)
claims := make([]matcherClaim, len(matchers))
for i, m := range matchers {
claims[i] = matcherClaim{TopicMatcher: m}
}
return claims
}
// createDeprecatedDummy builds a Hub exactly like createDummy, plus
// WithProtocolVersionCompatibility(8) so the test runs through the v8
// compat code path (bare-string JWT claims, `topic=` query parameter,
// deprecated subscription routes).
func createDeprecatedDummy(tb testing.TB, options ...Option) *Hub {
tb.Helper()
return createDummy(tb, append([]Option{WithProtocolVersionCompatibility(8)}, options...)...)
}
// createDeprecatedAuthorizedJWT mirrors createDummyAuthorizedJWT but emits
// v8 bare-string claims so topic selectors with `{var}` placeholders fall
// back to the URI-template matcher on the hub side.
//
//nolint:unparam // kept symmetric with createDummyAuthorizedJWT; role may be either.
func createDeprecatedAuthorizedJWT(r role, topics []string, payload ...any) string {
var p any = struct {
Foo string `json:"foo"`
}{Foo: "bar"}
if len(payload) > 0 {
p = payload[0]
}
token := jwt.New(jwt.SigningMethodHS256)
var key []byte
switch r {
case rolePublisher:
token.Claims = &claims{
deprecatedMercureClaims: deprecatedMercureClaims{Mercure: mercureClaim{Publish: stringsToDeprecatedClaims(topics)}},
RegisteredClaims: jwt.RegisteredClaims{},
}
key = []byte("publisher")
case roleSubscriber:
token.Claims = &claims{
deprecatedMercureClaims: deprecatedMercureClaims{Mercure: mercureClaim{
Subscribe: stringsToDeprecatedClaims(topics),
Payload: p,
}},
RegisteredClaims: jwt.RegisteredClaims{},
}
key = []byte("subscriber")
}
tokenString, _ := token.SignedString(key)
return tokenString
}