Skip to content

Commit 12e4c5b

Browse files
committed
update tests
1 parent abcfb58 commit 12e4c5b

3 files changed

Lines changed: 113 additions & 193 deletions

File tree

cmd/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ var rootCmd = &cobra.Command{
8888

8989
logger.Debugw("option", "connect-timeout", connectTimeout.String())
9090
logger.Debugw("option", "timeout", timeout.String())
91-
logger.Debugw("option", "pretendAsWeb", pretendAsWeb)
91+
logger.Debugw("option", "pretend-as-web", pretendAsWeb)
9292

9393
logger.Debugw("option", "proxy", proxyAddress)
94-
logger.Debugw("option", "certFile", certFile, "keyFile", keyFile)
94+
logger.Debugw("option", "cert-file", certFile, "key-file", keyFile)
9595

9696
server, err := httpproxy.NewServer(options...)
9797
if err != nil {

httpproxy/e2e_test.go

Lines changed: 55 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,43 @@ import (
1717
"github.com/stretchr/testify/require"
1818
)
1919

20+
func createProxy(require *require.Assertions, opts ...ServerOption) (chan struct{}, func()) {
21+
proxy, err := NewServer(opts...)
22+
require.Nil(err)
23+
24+
address := proxy.options.listenAddress
25+
if address == "" {
26+
address = fmt.Sprintf(":%d", proxy.options.listenPort)
27+
}
28+
29+
proxy.httpServer = &http.Server{
30+
Addr: address,
31+
Handler: proxy,
32+
}
33+
34+
ch := make(chan struct{}, 1)
35+
ln, err := net.Listen("tcp", address)
36+
require.Nil(err)
37+
38+
ch <- struct{}{}
39+
40+
certFile := proxy.options.certFile
41+
keyFile := proxy.options.keyFile
42+
if certFile != "" && keyFile != "" {
43+
logger.Infow("start listen with tls ...", "addr", address)
44+
go proxy.httpServer.ServeTLS(ln, certFile, keyFile)
45+
} else {
46+
logger.Infow("start listen ...", "addr", address)
47+
go proxy.httpServer.Serve(ln)
48+
}
49+
50+
return ch, func() {
51+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
52+
defer cancel()
53+
proxy.Shutdown(ctx)
54+
}
55+
}
56+
2057
func TestHTTP(t *testing.T) {
2158
require := require.New(t)
2259

@@ -27,18 +64,9 @@ func TestHTTP(t *testing.T) {
2764
}))
2865
defer upstream.Close()
2966

30-
proxy, err := NewServer(WithListenAddress(":8080"))
31-
require.Nil(err)
32-
l, err := proxy.Listen()
33-
require.Nil(err)
34-
35-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
36-
defer cancel()
37-
defer proxy.Shutdown(ctx)
38-
39-
go func() {
40-
proxy.Serve(l)
41-
}()
67+
ch, stop := createProxy(require, WithListenAddress(":8080"), WithPretendAsWeb(true))
68+
defer stop()
69+
<-ch
4270

4371
proxyUrl, err := url.Parse("http://127.0.0.1:8080")
4472
require.Nil(err)
@@ -71,18 +99,9 @@ func TestHTTPS(t *testing.T) {
7199
}))
72100
defer upstream.Close()
73101

74-
proxy, err := NewServer(WithListenAddress(":8080"))
75-
require.Nil(err)
76-
l, err := proxy.Listen()
77-
require.Nil(err)
78-
79-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
80-
defer cancel()
81-
defer proxy.Shutdown(ctx)
82-
83-
go func() {
84-
proxy.Serve(l)
85-
}()
102+
ch, stop := createProxy(require, WithListenAddress(":8080"), WithPretendAsWeb(true))
103+
defer stop()
104+
<-ch
86105

87106
proxyUrl, err := url.Parse("http://127.0.0.1:8080")
88107
require.Nil(err)
@@ -131,17 +150,9 @@ func TestTCP(t *testing.T) {
131150
}()
132151

133152
// proxy server
134-
proxy, err := NewServer(WithListenAddress(":8080"))
135-
require.Nil(err)
136-
l, err := proxy.Listen()
137-
require.Nil(err)
138-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
139-
defer cancel()
140-
defer proxy.Shutdown(ctx)
141-
142-
go func() {
143-
proxy.Serve(l)
144-
}()
153+
ch, stop := createProxy(require, WithListenAddress(":8080"), WithPretendAsWeb(true))
154+
defer stop()
155+
<-ch
145156

146157
// client
147158
conn, err := net.Dial("tcp", "127.0.0.1:8080")
@@ -173,59 +184,37 @@ func TestMock1(t *testing.T) {
173184
require := require.New(t)
174185

175186
// proxy server
176-
proxy, err := NewServer(
177-
WithListenAddress(":8080"),
178-
WithPretendAsWeb(true),
179-
)
180-
require.Nil(err)
181-
l, err := proxy.Listen()
182-
require.Nil(err)
183-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
184-
defer cancel()
185-
defer proxy.Shutdown(ctx)
186-
187-
go func() {
188-
proxy.Serve(l)
189-
}()
187+
ch, stop := createProxy(require, WithListenAddress(":8080"), WithPretendAsWeb(true))
188+
defer stop()
189+
<-ch
190190

191191
// client
192192
conn, err := net.Dial("tcp", "127.0.0.1:8080")
193193
require.Nil(err)
194194
defer conn.Close()
195195

196196
// req := fmt.Sprintf("GET %s HTTP/1.1\r\n\r\n\r\n", echoLn.Addr().String())
197-
req := fmt.Sprintf("GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", "/", "http://baidu.com")
197+
req := fmt.Sprintf("GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", "/", "baidu.com")
198198
_, err = conn.Write([]byte(req))
199199
require.Nil(err)
200200

201201
resp, err := http.ReadResponse(bufio.NewReader(conn), &http.Request{})
202202
require.Nil(err)
203203
defer resp.Body.Close()
204-
require.Equal(404, resp.StatusCode)
205204

206205
buf := make([]byte, 1024)
207206
n, _ := resp.Body.Read(buf)
208207
require.Equal("404 page not found\n", string(buf[:n]))
208+
209+
require.Equal(404, resp.StatusCode)
209210
}
210211

211212
func TestMock2(t *testing.T) {
212213
require := require.New(t)
213214

214-
proxy, err := NewServer(
215-
WithListenAddress(":8080"),
216-
WithPretendAsWeb(true),
217-
)
218-
require.Nil(err)
219-
l, err := proxy.Listen()
220-
require.Nil(err)
221-
222-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
223-
defer cancel()
224-
defer proxy.Shutdown(ctx)
225-
226-
go func() {
227-
proxy.Serve(l)
228-
}()
215+
ch, stop := createProxy(require, WithListenAddress(":8080"), WithPretendAsWeb(true))
216+
defer stop()
217+
<-ch
229218

230219
client := &http.Client{
231220
Transport: &http.Transport{

0 commit comments

Comments
 (0)