-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathlocal_test.go
100 lines (86 loc) · 2.63 KB
/
local_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
package cracker
import (
"io/ioutil"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestProxyConn(t *testing.T) {
startProxyServer()
h := &Handler{Server: "http://localhost" + testAddr, Secret: testSecret}
conn, err := h.Connect("localhost" + testAddr)
assert.NoError(t, err)
conn.Write([]byte("GET /ping HTTP/1.1\r\nHost: localhost\r\n\r\n"))
go func() {
time.Sleep(time.Millisecond * 100)
conn.Close()
}()
body, _ := ioutil.ReadAll(conn)
assert.Contains(t, string(body), "pong")
}
func TestProxyConn2(t *testing.T) {
startProxyServer()
h := &Handler{Server: "http://localhost" + testAddr, Secret: testSecret, Interval: time.Millisecond * 20}
conn, err := h.Connect("localhost" + testAddr)
assert.NoError(t, err)
conn.Write([]byte("GET /connect HTTP/1.1\r\nHost: localhost\r\n\r\n"))
go func() {
time.Sleep(time.Millisecond * 100)
conn.Close()
}()
body, _ := ioutil.ReadAll(conn)
assert.Contains(t, string(body), "404")
}
func TestProxyConn3(t *testing.T) {
startProxyServer()
h := &Handler{Server: "http://localhost" + testAddr, Secret: testSecret}
conn, err := h.Connect("localhost" + testAddr)
assert.NoError(t, err)
p, ok := conn.(*localProxyConn)
assert.True(t, ok)
// wrong uuid
p.uuid = ""
_, err = conn.Write([]byte("GET /connect HTTP/1.1\r\nHost: localhost\r\n\r\n"))
assert.Error(t, err)
assert.Contains(t, err.Error(), "uuid don't exist")
conn.Close()
}
func TestProxyConn4(t *testing.T) {
startProxyServer()
h := &Handler{Server: "http://localhost" + testAddr, Secret: testSecret, Interval: time.Millisecond * 20}
conn, err := h.Connect("localhost" + testAddr)
assert.NoError(t, err)
p, ok := conn.(*localProxyConn)
assert.True(t, ok)
// wrong uuid
p.uuid = ""
body, err := ioutil.ReadAll(conn)
assert.Error(t, err)
assert.Contains(t, err.Error(), "uuid don't exist")
conn.Close()
assert.Empty(t, body)
}
func TestProxyConn5(t *testing.T) {
startProxyServer()
h := &Handler{Server: "http://localhost" + testAddr, Secret: testSecret, Interval: time.Millisecond * 20}
conn, err := h.Connect("localhost" + testAddr)
assert.NoError(t, err)
p, ok := conn.(*localProxyConn)
assert.True(t, ok)
remote, ok := testP.proxyMap[p.uuid]
assert.True(t, ok)
assert.False(t, remote.IsClosed())
conn.Close()
}
func TestProxyConn6(t *testing.T) {
startProxyServer()
h := &Handler{Server: "http://localhost" + testAddr, Secret: testSecret, Interval: time.Millisecond * 20}
conn, err := h.Connect("localhost" + testAddr)
assert.NoError(t, err)
p, ok := conn.(*localProxyConn)
assert.True(t, ok)
remote, ok := testP.proxyMap[p.uuid]
assert.True(t, ok)
conn.Close()
assert.True(t, remote.IsClosed())
}