forked from yeqown/fasthttp-reverse-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws_reverseproxy_test.go
139 lines (117 loc) · 3.24 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
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
package proxy
import (
"fmt"
"net/http"
"testing"
"time"
"github.com/fasthttp/websocket"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"github.com/valyala/fasthttp"
)
func BenchmarkNewWSReverseProxy(b *testing.B) {
for i := 0; i < b.N; i++ {
p, err := NewWSReverseProxyWith(WithURL_OptionWS("ws://localhost:8080/echo"))
_ = p
_ = err
}
}
type wsTestSuite struct {
suite.Suite
server fasthttp.Server
}
func (w *wsTestSuite) SetupSuite() {
go w.backendProc(":8080")
time.Sleep(3 * time.Second)
}
func (w *wsTestSuite) backendProc(addr string) {
upgrader := websocket.FastHTTPUpgrader{}
echoHdl := func(ctx *fasthttp.RequestCtx) {
fmt.Printf("recv headers: %v\n", string(ctx.Request.Header.Header()))
err := upgrader.Upgrade(ctx, func(ws *websocket.Conn) {
defer ws.Close()
for {
mt, message, err := ws.ReadMessage()
if err != nil {
fmt.Printf("read message failed: %v\n", err)
break
}
fmt.Printf("recv: %s\n", message)
err = ws.WriteMessage(mt, message)
if err != nil {
fmt.Printf("write message failed: %v\n", err)
break
}
}
})
if err != nil {
if _, ok := err.(websocket.HandshakeError); ok {
fmt.Printf("websocket handshake: %v\n", 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
if err := server.ListenAndServe(addr); err != nil {
fmt.Printf("websocket backend server `ListenAndServe` quit, err=%v", err)
}
}
func executeAndAssert(t *testing.T, port int) {
// client
conn, resp, err := websocket.DefaultDialer.Dial(fmt.Sprintf("ws://localhost:%d", port), nil)
assert.Nil(t, err)
assert.Equal(t, http.StatusSwitchingProtocols, resp.StatusCode)
t.Logf("got resp: %+v", resp)
// client send
data := []byte("hello")
err = conn.WriteMessage(websocket.TextMessage, data)
assert.Nil(t, err)
// client read echo message
messageType, p, err := conn.ReadMessage()
assert.Nil(t, err)
assert.NotNil(t, p)
assert.Equal(t, websocket.TextMessage, messageType)
assert.NotZero(t, p)
assert.Equal(t, data, p)
}
func reverseProxyProc(p *WSReverseProxy, addr string) {
reqHdl := func(ctx *fasthttp.RequestCtx) {
p.ServeHTTP(ctx)
}
if err := fasthttp.ListenAndServe(addr, reqHdl); err != nil {
fmt.Printf("websocket proxy server `ListenAndServe` quit, err=%v\n", err)
}
}
func (w *wsTestSuite) Test_NewWSReverseProxyWith() {
p, err := NewWSReverseProxyWith(WithURL_OptionWS("ws://localhost:8080/echo"))
assert.Nil(w.T(), err)
assert.NotNil(w.T(), p)
go reverseProxyProc(p, ":8082")
executeAndAssert(w.T(), 8082)
}
func (w *wsTestSuite) Test_NewWSReverseProxyWith_WithForwardHeadersHandler() {
p, err := NewWSReverseProxyWith(
WithURL_OptionWS("ws://localhost:8080/echo"),
WithForwardHeadersHandlers_OptionWS(func(ctx *fasthttp.RequestCtx) (forwardHeader http.Header) {
return http.Header{
"X-TEST-HEAD": []string{"Test_NewWSReverseProxyWith_WithForwardHeadersHandler"},
}
}),
)
assert.Nil(w.T(), err)
go reverseProxyProc(p, ":8083")
executeAndAssert(w.T(), 8083)
}
func Test_wsTestSuite(t *testing.T) {
suite.Run(t, new(wsTestSuite))
}