-
Notifications
You must be signed in to change notification settings - Fork 1
/
all_test.go
119 lines (105 loc) · 2.53 KB
/
all_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
package commandproxy
import (
"context"
"net"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
)
func TestProxyCommandUnit(t *testing.T) {
s := httptest.NewServer(nil)
defer s.Close()
cli := s.Client()
cli.Transport = &http.Transport{
DialContext: func(ctx context.Context, _ string, address string) (net.Conn, error) {
conn, err := net.Dial("tcp", address)
if err != nil {
return nil, err
}
conn1, conn2 := net.Pipe()
var buf1, buf2 [32 * 1024]byte
go func() {
ctx, cancel := context.WithCancel(ctx)
err := Tunnel(ctx, &connWithCancel{conn, cancel}, conn1, buf1[:], buf2[:])
if err != nil {
t.Error(err)
return
}
}()
return conn2, nil
},
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
resp, err := cli.Get(s.URL)
if err != nil {
t.Fatal(err)
return
}
resp.Body.Close()
}
type connWithCancel struct {
net.Conn
cancel func()
}
func (c *connWithCancel) Close() error {
c.cancel()
return nil
}
func TestProxyCommand(t *testing.T) {
s := httptest.NewServer(nil)
defer s.Close()
cli := s.Client()
cli.Transport = &http.Transport{
DialContext: func(ctx context.Context, _ string, address string) (net.Conn, error) {
proxy := ProxyCommand(ctx, "go", "run", "./cmd/commandproxy", s.Listener.Addr().String())
proxy.Stderr = os.Stderr
return proxy.Stdio()
},
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
resp, err := cli.Get(s.URL)
if err != nil {
t.Fatal(err)
return
}
resp.Body.Close()
}
func TestProxyCommandExit(t *testing.T) {
s := httptest.NewServer(nil)
defer s.Close()
cli := s.Client()
cli.Transport = &http.Transport{
DialContext: func(ctx context.Context, _ string, address string) (net.Conn, error) {
proxy := ProxyCommand(ctx, "go", "run", "./cmd/commandproxy")
proxy.Stderr = os.Stderr
return proxy.Stdio()
},
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
_, err := cli.Get(s.URL)
if err == nil {
t.Fail()
}
}
func TestProxyCommandNotFoundCmd(t *testing.T) {
proxy := ProxyCommand(context.Background(), "./notfound")
proxy.Stderr = os.Stderr
_, err := proxy.Stdio()
if err == nil {
t.Fail()
}
}