Skip to content

Commit e92019b

Browse files
committed
tests(ws_reverseproxy): use test suite instead of unit tests of function
1 parent cb14ece commit e92019b

File tree

4 files changed

+42
-45
lines changed

4 files changed

+42
-45
lines changed

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
module github.com/yeqown/fasthttp-reverse-proxy/v2
22

33
require (
4-
github.com/andybalholm/brotli v1.0.3 // indirect
54
github.com/fasthttp/websocket v1.4.3
6-
github.com/klauspost/compress v1.13.6 // indirect
75
github.com/stretchr/testify v1.7.0
86
github.com/valyala/fasthttp v1.30.0
97
github.com/yeqown/log v1.1.1

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
2+
github.com/andybalholm/brotli v1.0.2 h1:JKnhI/XQ75uFBTiuzXpzFrUriDPiZjlOSzh6wXogP0E=
23
github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
3-
github.com/andybalholm/brotli v1.0.3 h1:fpcw+r1N1h0Poc1F/pHbW40cUm/lMEQslZtCkBQ0UnM=
4-
github.com/andybalholm/brotli v1.0.3/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
54
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
65
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
76
github.com/fasthttp/websocket v1.4.3 h1:qjhRJ/rTy4KB8oBxljEC00SDt6HUY9jLRfM601SUdS4=
87
github.com/fasthttp/websocket v1.4.3/go.mod h1:5r4oKssgS7W6Zn6mPWap3NWzNPJNzUUh3baWTOhcYQk=
98
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
109
github.com/klauspost/compress v1.10.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
10+
github.com/klauspost/compress v1.13.4 h1:0zhec2I8zGnjWcKyLl6i3gPqKANCCn5e9xmviEEeX6s=
1111
github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
12-
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
13-
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
1412
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
1513
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1614
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

ws_reverseproxy.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"net"
88
"net/http"
9+
"strings"
910

1011
"github.com/yeqown/log"
1112

@@ -39,6 +40,7 @@ type WSReverseProxy struct {
3940
// Deprecated.
4041
// NewWSReverseProxyWith is recommended.
4142
func NewWSReverseProxy(host, path string) *WSReverseProxy {
43+
path = strings.TrimPrefix(path, "/")
4244
wsproxy, err := NewWSReverseProxyWith(
4345
WithURL_OptionWS(fmt.Sprintf("%s://%s/%s", "ws", host, path)),
4446
)

ws_reverseproxy_test.go

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package proxy
22

33
import (
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

Comments
 (0)