-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnsocket_test.go
More file actions
142 lines (128 loc) · 3.36 KB
/
nsocket_test.go
File metadata and controls
142 lines (128 loc) · 3.36 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package nsocket_test
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"sync"
"testing"
"time"
"github.com/epikoder/nsocket"
"github.com/google/uuid"
"github.com/gorilla/websocket"
"github.com/olahol/melody"
)
const (
dialAndConnectTimeout = 5 * time.Second
origin = "http://localhost:8000/ws"
url = "ws://localhost:8000/ws"
)
func TestNSocket(t *testing.T) {
authKey := uuid.New().String()
cookie := []string{"auth=" + authKey}
mux := http.NewServeMux()
// Configure Nsocket server
soc := nsocket.New(nsocket.Config{
AuthFunc: func(r *http.Request) (ok bool) {
c, err := r.Cookie("auth")
if err != nil {
return
}
return c.Value == authKey
},
AllowedOrigins: []string{"localhost:3000", "localhost:8000"},
Namespace: nsocket.Namespace{
nsocket.Default: nsocket.Event{
"/": func(s *melody.Session, i interface{}, soc *nsocket.NSocket) {
fmt.Printf("Namespace: [Default] -- GOT: %v ---- from ---- %v\n", i, s.RemoteAddr())
if err := soc.Broadcast("namespace:default -- "+fmt.Sprintf("%v ------> %v", i, s.RemoteAddr()), s); err != nil {
t.Error(err)
}
},
"message": func(s *melody.Session, i interface{}, soc *nsocket.NSocket) {
fmt.Printf("Namespace: [Default/Message] -- GOT: %v ---- from ---- %v\n", i, s.RemoteAddr())
if err := soc.Emit("namespace:message -- "+fmt.Sprintf("%v ------> %v", i, s.RemoteAddr()), "message"); err != nil {
t.Error(err)
}
},
},
},
})
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if err := soc.Serve(w, r); err != nil {
t.Error(err)
}
})
fmt.Println("starting server on: http://localhost:8000")
go http.ListenAndServe(":8000", mux)
type Client = struct {
Ctx context.Context
Cancel context.CancelFunc
Conn *websocket.Conn
}
var client Client
var wg sync.WaitGroup
var reqHeader http.Header = http.Header{
"Cookie": cookie,
}
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(dialAndConnectTimeout))
conn, _, err := websocket.DefaultDialer.Dial(url, reqHeader)
if err != nil {
panic(err)
}
client = Client{
ctx, cancel, conn,
}
client.Conn.EnableWriteCompression(true)
go func() {
for {
var v interface{}
// err := conn.ReadJSON(&v)
_, b, err := client.Conn.ReadMessage()
if err != nil && err != io.EOF {
panic(err)
}
if err = json.Unmarshal(b, &v); err != nil {
fmt.Println(err)
}
fmt.Printf("Client:: - %v \n", v)
wg.Done()
}
}()
wg.Add(1)
client.Conn.WriteJSON(map[string]interface{}{
"id": uuid.New(),
"type": "nsocket",
"action": "subscribe",
"namespace": "message",
})
wg.Add(1)
client.Conn.WriteJSON(map[string]interface{}{
"id": uuid.New(),
"type": "emit",
"body": "I should not be received",
"namespace": "not-found",
})
wg.Add(1)
client.Conn.WriteJSON(map[string]interface{}{
"id": uuid.New(),
"type": "emit",
"body": "I should be received in namspace::message",
"namespace": "message",
})
wg.Add(1)
client.Conn.WriteJSON(map[string]interface{}{
"id": uuid.New(),
"type": "emit",
"body": "I should be received in namspace::default",
})
wg.Add(1)
client.Conn.WriteJSON(map[string]interface{}{
"id": uuid.New(),
"type": "nsocket",
"action": "unsubscribe",
"namespace": "message",
})
wg.Wait()
}