-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbridge_test.go
More file actions
128 lines (107 loc) · 3.25 KB
/
Copy pathbridge_test.go
File metadata and controls
128 lines (107 loc) · 3.25 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
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
package main
import (
"encoding/json"
"testing"
)
func TestInjectToken_ConnectFrame(t *testing.T) {
input := `{"type":"req","method":"connect","params":{}}`
result := injectToken([]byte(input), "mytoken")
var f map[string]any
if err := json.Unmarshal(result, &f); err != nil {
t.Fatalf("unmarshal result: %v", err)
}
params, ok := f["params"].(map[string]any)
if !ok {
t.Fatalf("params is not a map")
}
auth, ok := params["auth"].(map[string]any)
if !ok {
t.Fatalf("params.auth is not a map")
}
tok, ok := auth["token"].(string)
if !ok {
t.Fatalf("params.auth.token is not a string")
}
if tok != "mytoken" {
t.Errorf("params.auth.token = %q, want %q", tok, "mytoken")
}
}
func TestInjectToken_ScopesInjected(t *testing.T) {
input := `{"type":"req","method":"connect","params":{}}`
result := injectToken([]byte(input), "mytoken")
var f map[string]any
if err := json.Unmarshal(result, &f); err != nil {
t.Fatalf("unmarshal result: %v", err)
}
params := f["params"].(map[string]any)
scopes, ok := params["scopes"].([]any)
if !ok {
t.Fatalf("params.scopes is not an array: %T", params["scopes"])
}
if len(scopes) != 1 || scopes[0] != "operator.admin" {
t.Errorf("params.scopes = %v, want [operator.admin]", scopes)
}
}
func TestInjectToken_ScopesPreserved(t *testing.T) {
input := `{"type":"req","method":"connect","params":{"scopes":["operator.read","operator.write"]}}`
result := injectToken([]byte(input), "mytoken")
var f map[string]any
if err := json.Unmarshal(result, &f); err != nil {
t.Fatalf("unmarshal result: %v", err)
}
params := f["params"].(map[string]any)
scopes, ok := params["scopes"].([]any)
if !ok {
t.Fatalf("params.scopes is not an array: %T", params["scopes"])
}
if len(scopes) != 2 {
t.Errorf("params.scopes = %v, want [operator.read operator.write] (preserved)", scopes)
}
}
func TestInjectToken_EmptyTokenPassthrough(t *testing.T) {
input := `{"type":"req","method":"connect","params":{}}`
result := injectToken([]byte(input), "")
if string(result) != input {
t.Errorf("empty token should pass through unchanged")
}
}
func TestInjectToken_NonConnectFrame(t *testing.T) {
input := `{"type":"ping"}`
result := injectToken([]byte(input), "mytoken")
// Output should be unchanged from the input.
var orig, got map[string]any
json.Unmarshal([]byte(input), &orig)
if err := json.Unmarshal(result, &got); err != nil {
t.Fatalf("unmarshal result: %v", err)
}
if got["type"] != "ping" {
t.Errorf("type = %v, want %q", got["type"], "ping")
}
if _, exists := got["params"]; exists {
t.Errorf("non-connect frame should not have params added")
}
}
func TestInjectToken_InvalidJSON(t *testing.T) {
input := "not json"
result := injectToken([]byte(input), "mytoken")
if string(result) != input {
t.Errorf("result = %q, want %q (unchanged)", string(result), input)
}
}
func TestToWS(t *testing.T) {
tests := []struct {
input string
want string
}{
{"http://localhost", "ws://localhost"},
{"https://localhost", "wss://localhost"},
{"http://example.com:8080/path", "ws://example.com:8080/path"},
{"https://example.com/path", "wss://example.com/path"},
}
for _, tt := range tests {
got := toWS(tt.input)
if got != tt.want {
t.Errorf("toWS(%q) = %q, want %q", tt.input, got, tt.want)
}
}
}