forked from jweslley/localtunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocaltunnel_test.go
162 lines (127 loc) · 3.7 KB
/
localtunnel_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package localtunnel
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"net/url"
"regexp"
"strconv"
"testing"
)
var ltRegexp = regexp.MustCompile("^https:\\/\\/.*\\.localtunnel.me$")
func TestDefaultClient(t *testing.T) {
if DefaultClient == nil {
t.Fatal("DefaultClient can not be null")
}
if DefaultClient.endPoint != "https://localtunnel.me" {
t.Fatalf("Unexpected default remote host: %s", DefaultClient.endPoint)
}
}
func TestSetupLocalTunnel(t *testing.T) {
content := "Hello from local server!"
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
fmt.Fprint(w, content)
}))
localPort := getServerPort(t, s)
tunnel := NewLocalTunnel(localPort)
checkTunnelIsNotConnected(t, tunnel, localPort)
err := tunnel.Open()
if err != nil {
t.Fatalf("Cannot open tunnel: %s", err)
}
checkTunnelIsConnected(t, tunnel, localPort)
for i := 0; i <= tunnel.MaxConn()*2; i++ {
response, err := readFromURL(tunnel.URL())
if err != nil {
t.Fatalf("Cannot connect through the tunnel: %s", err)
}
if response != content {
t.Fatalf("Unexpected response. Expected: '%s'. Actual: '%s'", content, response)
}
}
tunnel.Close()
<-tunnel.Closing()
checkTunnelIsNotConnected(t, tunnel, localPort)
}
func checkTunnelIsNotConnected(t *testing.T, tunnel *Tunnel, localPort int) {
if tunnel.RemoteHost() != "" {
t.Fatalf("Remote host should be empty: %s", tunnel.RemoteHost())
}
if tunnel.RemotePort() != 0 {
t.Fatalf("Remote port should be zero. Actual: %d", tunnel.RemotePort())
}
if tunnel.LocalHost() != "localhost" {
t.Fatalf("Local host should be localhost. Actual: %s", tunnel.LocalHost())
}
if tunnel.LocalPort() != localPort {
t.Fatalf("Unexpected local port. Expected: %d, Actual: %d", localPort, tunnel.LocalPort())
}
if tunnel.Subdomain() != "" {
t.Fatalf("Subdomain should be empty. Actual: %s", tunnel.Subdomain())
}
if tunnel.URL() != "" {
t.Fatalf("URL should be empty. Actual: %s", tunnel.URL())
}
if tunnel.MaxConn() != 0 {
t.Fatalf("Max connections should be zero. Actual: %d", tunnel.MaxConn())
}
}
func checkTunnelIsConnected(t *testing.T, tunnel *Tunnel, localPort int) {
if tunnel.RemoteHost() == "" {
t.Fatalf("Remote host should not be empty: %s", tunnel.RemoteHost())
}
if tunnel.RemotePort() <= 0 {
t.Fatalf("Remote port should be greater than zero. Actual: %d", tunnel.RemotePort())
}
if tunnel.LocalHost() != "localhost" {
t.Fatalf("Local host should be localhost. Actual: %s", tunnel.LocalHost())
}
if tunnel.LocalPort() != localPort {
t.Fatalf("Unexpected local port. Expected: %d, Actual: %d", localPort, tunnel.LocalPort())
}
if tunnel.Subdomain() == "" {
t.Fatalf("Subdomain should not be empty. Actual: %s", tunnel.Subdomain())
}
if tunnel.URL() == "" {
t.Fatalf("URL should not be empty. Actual: %s", tunnel.URL())
}
if !ltRegexp.MatchString(tunnel.URL()) {
t.Fatalf("URL should match '%s'. Actual: %s", ltRegexp, tunnel.URL())
}
if tunnel.MaxConn() <= 0 {
t.Fatalf("Max connections should be greater than zero. Actual: %d", tunnel.MaxConn())
}
}
func readFromURL(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(bytes), nil
}
func getServerPort(t *testing.T, s *httptest.Server) int {
url, e := url.Parse(s.URL)
if e != nil {
t.Fatal(e)
}
_, portStr, e := net.SplitHostPort(url.Host)
if e != nil {
t.Fatal(e)
}
port, err := strconv.Atoi(portStr)
if err != nil {
t.Fatal(e)
}
if port <= 0 {
t.Fatalf("Local port should be greater than zero. Actual: %d", port)
}
return port
}