-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathlocaltunnel_test.go
116 lines (101 loc) · 2.66 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
package localtunnel
import (
"bytes"
"crypto/rand"
"fmt"
"io/ioutil"
"net/http"
"testing"
"time"
)
type testLog struct {
*testing.T
}
func (t testLog) Println(v ...interface{}) {
t.Log(v...)
}
// testLocalTunnel against server at baseURL (if given)
func testLocalTunnel(baseURL string, t *testing.T) {
// Collect some random data
random := make([]byte, 4*1024*1024) // 4 MiB
if _, err := rand.Read(random); err != nil {
t.Fatal("failed to generate random data, error:", err)
}
// Setup a test server that we wish to expose
port := 60000
server := http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if r.URL.Query().Get("random") == "true" {
w.Write(random)
} else {
w.Write([]byte("hello-world"))
}
}),
}
go server.ListenAndServe()
defer server.Close()
t.Log("setting up LocalTunnel")
lt, err := New(port, "", Options{
Log: testLog{t},
MaxConnections: 2,
BaseURL: baseURL,
})
if err != nil {
t.Fatal("failed to create LocalTunnel, error: ", err)
}
// Sleep for 3s giving the server time to register
time.Sleep(3 * time.Second)
// Make http.Client with timeout of 30 seconds
client := &http.Client{
Timeout: 30 * time.Second,
}
// Let's make 3 requests for good measure
for i := 0; i < 3; i++ {
t.Log("sending test request to:", lt.URL())
var res *http.Response
res, err = client.Get(lt.URL())
if err != nil {
t.Fatal("failed to send GET request through tunnel, error: ", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
t.Error("expected 200 ok, got status: ", res.StatusCode)
}
var data []byte
data, err = ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal("failed to read response from tunnel, error: ", err)
}
if string(data) != "hello-world" {
t.Error("unexpected response, data: ", string(data))
}
}
t.Log("sending testing request to:", lt.URL()+"/?random=true")
var res *http.Response
res, err = client.Get(lt.URL() + "/?random=true")
if err != nil {
t.Fatal("failed to send GET request through tunnel, error: ", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
t.Error("expected 200 ok, got status: ", res.StatusCode)
}
var data []byte
data, err = ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal("failed to read response from tunnel, error: ", err)
}
if bytes.Compare(data, random) != 0 {
t.Error("unexpected random response, size:", len(data))
}
t.Log("closing LocalTunnel")
err = lt.Close()
if err != nil {
t.Error("error closing the tunnel: ", err)
}
}
func TestLocalTunnel(t *testing.T) {
testLocalTunnel("", t)
}