11package proxy
22
33import (
4+ "fmt"
45 "net/http"
56 "testing"
67 "time"
78
89 "github.com/fasthttp/websocket"
910 "github.com/stretchr/testify/assert"
11+ "github.com/stretchr/testify/suite"
1012 "github.com/valyala/fasthttp"
1113 "github.com/yeqown/log"
1214)
@@ -18,9 +20,21 @@ func BenchmarkNewWSReverseProxy(b *testing.B) {
1820 }
1921}
2022
21- func runBackend (addr string ) {
23+ type wsTestSuite struct {
24+ suite.Suite
25+
26+ server fasthttp.Server
27+ }
28+
29+ func (w * wsTestSuite ) SetupSuite () {
30+ go w .backendProc (":8080" )
31+
32+ time .Sleep (3 * time .Second )
33+ }
34+
35+ func (w * wsTestSuite ) backendProc (addr string ) {
2236 upgrader := websocket.FastHTTPUpgrader {}
23- entry := logger .WithField ("func" , "runBackend " )
37+ entry := logger .WithField ("func" , "backendProc " )
2438 echoHdl := func (ctx * fasthttp.RequestCtx ) {
2539 entry .
2640 WithFields (log.Fields {
@@ -70,9 +84,9 @@ func runBackend(addr string) {
7084 }
7185}
7286
73- func doRequest (t * testing.T ) {
87+ func executeAndAssert (t * testing.T , port int ) {
7488 // client
75- conn , resp , err := websocket .DefaultDialer .Dial ("ws://localhost:8081" , nil )
89+ conn , resp , err := websocket .DefaultDialer .Dial (fmt . Sprintf ( "ws://localhost:%d" , port ) , nil )
7690 assert .Nil (t , err )
7791 assert .Equal (t , http .StatusSwitchingProtocols , resp .StatusCode )
7892 t .Logf ("got resp: %+v" , resp )
@@ -91,7 +105,7 @@ func doRequest(t *testing.T) {
91105 assert .Equal (t , data , p )
92106}
93107
94- func runProxy (p * WSReverseProxy , addr string ) {
108+ func reverseProxyProc (p * WSReverseProxy , addr string ) {
95109 reqHdl := func (ctx * fasthttp.RequestCtx ) {
96110 p .ServeHTTP (ctx )
97111 }
@@ -101,43 +115,27 @@ func runProxy(p *WSReverseProxy, addr string) {
101115 }
102116}
103117
104- func Test_NewWSReverseProxy (t * testing.T ) {
105- go runBackend (":8080" )
106- time .Sleep (3 * time .Second )
107-
108- // constructs a websocket proxy server
118+ func (w * wsTestSuite ) Test_NewWSReverseProxy () {
109119 var p * WSReverseProxy
110- assert .NotPanics (t , func () {
120+ assert .NotPanics (w . T () , func () {
111121 p = NewWSReverseProxy ("localhost:8080" , "/echo" )
112- }, "compatiable old version API failed" )
113- assert .NotNil (t , p )
114-
115- // star and serve
116- go runProxy (p , ":8081" )
122+ }, "compatible old version API failed" )
123+ assert .NotNil (w .T (), p )
117124
118- doRequest (t )
125+ go reverseProxyProc (p , ":8081" )
126+ executeAndAssert (w .T (), 8081 )
119127}
120128
121- func Test_NewWSReverseProxyWith (t * testing.T ) {
122- go runBackend (":8080" )
123-
124- time .Sleep (3 * time .Second )
125- // constructs a websocket proxy server
129+ func (w * wsTestSuite )Test_NewWSReverseProxyWith () {
126130 p , err := NewWSReverseProxyWith (WithURL_OptionWS ("ws://localhost:8080/echo" ))
127- assert .Nil (t , err )
128- assert .NotNil (t , p )
129-
130- // star and serve
131- go runProxy (p , ":8081" )
131+ assert .Nil (w .T (), err )
132+ assert .NotNil (w .T (), p )
132133
133- doRequest (t )
134+ go reverseProxyProc (p , ":8082" )
135+ executeAndAssert (w .T (), 8082 )
134136}
135137
136- func Test_NewWSReverseProxyWith_WithForwardHeadersHandler (t * testing.T ) {
137- go runBackend (":8080" )
138-
139- time .Sleep (3 * time .Second )
140- // constructs a websocket proxy server
138+ func (w * wsTestSuite ) Test_NewWSReverseProxyWith_WithForwardHeadersHandler () {
141139 p , err := NewWSReverseProxyWith (
142140 WithURL_OptionWS ("ws://localhost:8080/echo" ),
143141 WithForwardHeadersHandlers_OptionWS (func (ctx * fasthttp.RequestCtx ) (forwardHeader http.Header ) {
@@ -146,11 +144,12 @@ func Test_NewWSReverseProxyWith_WithForwardHeadersHandler(t *testing.T) {
146144 }
147145 }),
148146 )
149- assert .Nil (t , err )
147+ assert .Nil (w . T () , err )
150148
151- // star and serve
152- go runProxy (p , ":8081" )
153-
154- // doRequest
155- doRequest (t )
149+ go reverseProxyProc (p , ":8083" )
150+ executeAndAssert (w .T (), 8083 )
156151}
152+
153+ func Test_wsTestSuite (t * testing.T ) {
154+ suite .Run (t , new (wsTestSuite ))
155+ }
0 commit comments