-
Notifications
You must be signed in to change notification settings - Fork 4
/
all_test.go
79 lines (67 loc) · 1.42 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
package sshproxy
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
_ "github.com/wzshiming/sshd/directstreamlocal"
_ "github.com/wzshiming/sshd/directtcp"
_ "github.com/wzshiming/sshd/streamlocalforward"
_ "github.com/wzshiming/sshd/tcpforward"
)
var testServer = httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("ok"))
}))
func TestBind(t *testing.T) {
s, err := NewSimpleServer("ssh://u:p@:0")
if err != nil {
t.Fatal(err)
}
err = s.Start(context.Background())
if err != nil {
t.Fatal(err)
}
defer s.Close()
dial, err := NewDialer(s.ProxyURL())
if err != nil {
t.Fatal(err)
}
defer dial.Close()
listener, err := dial.Listen(context.Background(), "tcp", ":10000")
if err != nil {
t.Fatal(err)
}
go http.Serve(listener, nil)
time.Sleep(time.Second / 10)
resp, err := http.Get("http://127.0.0.1:10000")
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
}
func TestServer(t *testing.T) {
s, err := NewSimpleServer("ssh://u:p@:0")
if err != nil {
t.Fatal(err)
}
err = s.Start(context.Background())
if err != nil {
t.Fatal(err)
}
defer s.Close()
dial, err := NewDialer(s.ProxyURL())
if err != nil {
t.Fatal(err)
}
defer dial.Close()
cli := testServer.Client()
cli.Transport = &http.Transport{
DialContext: dial.DialContext,
}
resp, err := cli.Get(testServer.URL)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
}