-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontext_utils_test.go
More file actions
94 lines (74 loc) · 2.44 KB
/
context_utils_test.go
File metadata and controls
94 lines (74 loc) · 2.44 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
package velaros_test
import (
"encoding/json"
"testing"
"github.com/RobertWHurst/velaros"
jsonMiddleware "github.com/RobertWHurst/velaros/middleware/json"
)
func Test_CtxSocket(t *testing.T) {
socket := velaros.NewSocket(&velaros.ConnectionInfo{}, nil)
inboundMsg := &velaros.InboundMessage{RawData: []byte("{}")}
ctx := velaros.NewContext(socket, inboundMsg, func(ctx *velaros.Context) {})
retrievedSocket := velaros.CtxSocket(ctx)
if retrievedSocket != socket {
t.Error("expected CtxSocket to return the same socket instance")
}
if retrievedSocket.ID() != socket.ID() {
t.Errorf("expected socket ID %s, got %s", socket.ID(), retrievedSocket.ID())
}
}
func Test_CtxMeta_WithMeta(t *testing.T) {
msgData := map[string]any{
"id": "test-msg",
"path": "/test",
"meta": map[string]any{
"userId": "user-123",
"traceId": "trace-456",
},
}
msgBytes, _ := json.Marshal(msgData)
socket := velaros.NewSocket(&velaros.ConnectionInfo{}, nil)
inboundMsg := &velaros.InboundMessage{RawData: msgBytes}
var capturedMeta map[string]any
ctx := velaros.NewContext(socket, inboundMsg, func(ctx *velaros.Context) {
capturedMeta = velaros.CtxMeta(ctx)
})
middleware := jsonMiddleware.Middleware()
middleware(ctx)
if capturedMeta == nil {
t.Fatal("expected CtxMeta to return meta map")
}
if capturedMeta["userId"] != "user-123" {
t.Errorf("expected userId=user-123, got %v", capturedMeta["userId"])
}
if capturedMeta["traceId"] != "trace-456" {
t.Errorf("expected traceId=trace-456, got %v", capturedMeta["traceId"])
}
}
func Test_CtxMeta_WithoutMeta(t *testing.T) {
msgData := map[string]any{
"id": "test-msg",
"path": "/test",
}
msgBytes, _ := json.Marshal(msgData)
socket := velaros.NewSocket(&velaros.ConnectionInfo{}, nil)
inboundMsg := &velaros.InboundMessage{RawData: msgBytes}
var capturedMeta map[string]any
ctx := velaros.NewContext(socket, inboundMsg, func(ctx *velaros.Context) {
capturedMeta = velaros.CtxMeta(ctx)
})
middleware := jsonMiddleware.Middleware()
middleware(ctx)
if capturedMeta != nil {
t.Errorf("expected CtxMeta to return nil, got %v", capturedMeta)
}
}
func Test_CtxFree(t *testing.T) {
socket := velaros.NewSocket(&velaros.ConnectionInfo{}, nil)
inboundMsg := &velaros.InboundMessage{RawData: []byte("{}")}
ctx := velaros.NewContext(socket, inboundMsg, func(ctx *velaros.Context) {})
velaros.CtxFree(ctx)
if ctx.Error != nil {
t.Errorf("unexpected error after free: %v", ctx.Error)
}
}