forked from yeqown/fasthttp-reverse-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws_reverseproxy_test.go
109 lines (97 loc) · 2.27 KB
/
ws_reverseproxy_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
package proxy
import (
"bytes"
"log"
"net/http"
"testing"
"github.com/fasthttp/websocket"
"github.com/valyala/fasthttp"
)
func BenchmarkNewWSReverseProxy(b *testing.B) {
for i := 0; i < b.N; i++ {
p := NewWSReverseProxy("localhost", "/path")
_ = p
}
}
func Test_WSReverseProxy(t *testing.T) {
upgrader := websocket.FastHTTPUpgrader{}
echoHdl := func(ctx *fasthttp.RequestCtx) {
err := upgrader.Upgrade(ctx, func(ws *websocket.Conn) {
defer ws.Close()
for {
mt, message, err := ws.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}
log.Printf("recv: %s", message)
err = ws.WriteMessage(mt, message)
if err != nil {
log.Println("write:", err)
break
}
}
})
if err != nil {
if _, ok := err.(websocket.HandshakeError); ok {
log.Println(err)
}
return
}
}
// backend server initializing
server := fasthttp.Server{
Name: "Name",
Handler: func(ctx *fasthttp.RequestCtx) {
switch string(ctx.Path()) {
case "/echo":
echoHdl(ctx)
}
},
}
// backend websocket server
go func() {
if err := server.ListenAndServe(":8080"); err != nil {
logger.Errorf("websocket backend server `ListenAndServe` quit, err=%v", err)
}
}()
// start websocket proxy server
p := NewWSReverseProxy("localhost:8080", "/echo")
go func() {
reqHdl := func(ctx *fasthttp.RequestCtx) {
p.ServeHTTP(ctx)
}
if err := fasthttp.ListenAndServe(":8081", reqHdl); err != nil {
logger.Errorf("websocket proxy server `ListenAndServe` quit, err=%v", err)
}
}()
// client
conn, resp, err := websocket.DefaultDialer.Dial("ws://localhost:8081", nil)
if err != nil {
t.Error(err)
t.FailNow()
}
t.Log(resp)
if resp.StatusCode != http.StatusSwitchingProtocols {
t.Error("could not connect to proxy")
t.FailNow()
}
// client send
sendmsg := []byte("hello")
err = conn.WriteMessage(websocket.TextMessage, sendmsg)
if err != nil {
t.Error(err)
t.FailNow()
}
// client read echo message
messageType, recvmsg, err := conn.ReadMessage()
if err != nil {
t.Error(err)
t.FailNow()
}
if messageType != websocket.TextMessage || !bytes.Equal(sendmsg, recvmsg) {
t.Errorf("recv message not wanted: [%v / %v], [%s / %s]",
messageType, websocket.TextMessage, recvmsg, sendmsg)
t.FailNow()
}
}