Skip to content

Commit 454f7cc

Browse files
author
Jia Zhan
committed
add http1 test
1 parent 0811cf3 commit 454f7cc

File tree

2 files changed

+118
-5
lines changed

2 files changed

+118
-5
lines changed

http1_test.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package stream
2+
3+
import (
4+
"fmt"
5+
"net"
6+
"net/http"
7+
"net/http/httputil"
8+
"net/url"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
const (
16+
http1Port = 6666
17+
http1ProxyPort = 16666
18+
)
19+
20+
func TestHTTP1(t *testing.T) {
21+
22+
tests := []struct {
23+
msg string
24+
port int
25+
abortServerHandler bool
26+
wantErr string
27+
}{
28+
{
29+
msg: "direct",
30+
port: http1Port,
31+
},
32+
{
33+
msg: "via proxy",
34+
port: http1ProxyPort,
35+
},
36+
{
37+
msg: "direct, server panic",
38+
port: http1Port,
39+
abortServerHandler: true,
40+
wantErr: "EOF",
41+
},
42+
{
43+
msg: "via proxy, server panic",
44+
port: http1ProxyPort,
45+
abortServerHandler: true,
46+
wantErr: "EOF",
47+
},
48+
}
49+
50+
for _, tt := range tests {
51+
t.Run(tt.msg, func(t *testing.T) {
52+
s := startHTTPServer(t, tt.abortServerHandler)
53+
defer s.Close()
54+
55+
req, err := http.NewRequest("GET", fmt.Sprintf("http://127.0.0.1:%d", tt.port), nil)
56+
require.NoError(t, err)
57+
58+
proxyServer := startHTTP1ReverseProxy(t)
59+
defer proxyServer.Close()
60+
61+
resp, err := http.DefaultClient.Do(req)
62+
t.Logf("got err: %v", err)
63+
t.Logf("got resp: %v", resp)
64+
if tt.wantErr != "" {
65+
require.Error(t, err)
66+
assert.Contains(t, err.Error(), tt.wantErr)
67+
return
68+
}
69+
require.NoError(t, err)
70+
assert.Contains(t, getResponseBody(t, resp), "hello world HTTP/1.1")
71+
defer resp.Body.Close()
72+
})
73+
}
74+
}
75+
76+
func startHTTPServer(t *testing.T, abortHandler bool) *http.Server {
77+
h1Handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
78+
fmt.Fprintln(w, "hello world "+r.Proto)
79+
if abortHandler {
80+
panic(http.ErrAbortHandler)
81+
}
82+
})
83+
84+
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", http1Port))
85+
require.NoError(t, err)
86+
87+
server := &http.Server{
88+
Handler: h1Handler,
89+
}
90+
91+
go server.Serve(ln)
92+
93+
return server
94+
}
95+
96+
func startHTTP1ReverseProxy(t *testing.T) *http.Server {
97+
rpURL, err := url.Parse(fmt.Sprintf("http://localhost:%d", http1Port))
98+
require.NoError(t, err)
99+
100+
proxy := httputil.NewSingleHostReverseProxy(rpURL)
101+
proxy.Transport = &http.Transport{}
102+
103+
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", http1ProxyPort))
104+
require.NoError(t, err)
105+
106+
proxyServer := &http.Server{
107+
Handler: proxy,
108+
}
109+
110+
go proxyServer.Serve(ln)
111+
112+
return proxyServer
113+
}

http2_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import (
1818
)
1919

2020
const (
21-
http2Port = 7777
22-
proxyPort = 17777
21+
http2Port = 7777
22+
http2ProxyPort = 17777
2323
)
2424

2525
func TestHTTP2(t *testing.T) {
@@ -36,7 +36,7 @@ func TestHTTP2(t *testing.T) {
3636
},
3737
{
3838
msg: "via proxy, success",
39-
port: proxyPort,
39+
port: http2ProxyPort,
4040
},
4141
{
4242
msg: "direct, server panic",
@@ -46,7 +46,7 @@ func TestHTTP2(t *testing.T) {
4646
},
4747
{
4848
msg: "via proxy, server panic",
49-
port: proxyPort,
49+
port: http2ProxyPort,
5050
abortServerHandler: true,
5151
wantErr: "stream error",
5252
},
@@ -125,7 +125,7 @@ func startHTTP2ReverseProxy(t *testing.T) *http.Server {
125125
},
126126
}
127127

128-
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", proxyPort))
128+
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", http2ProxyPort))
129129
require.NoError(t, err)
130130

131131
proxyServer := &http.Server{

0 commit comments

Comments
 (0)