-
Notifications
You must be signed in to change notification settings - Fork 1
/
e2e_test.go
102 lines (87 loc) · 2.15 KB
/
e2e_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
package tcprouter
import (
"context"
"crypto/rand"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/url"
"sync"
"testing"
"time"
"github.com/magiconair/properties/assert"
"github.com/stretchr/testify/require"
)
func TestEnd2End(t *testing.T) {
sizes := []int{1, 128}
for _, size := range sizes {
name := fmt.Sprintf("%dMiB", size)
t.Run(name, func(t *testing.T) {
testEnd2End(t, size)
})
}
}
func testEnd2End(t *testing.T, size int) {
var (
domain = "localhost"
secret = "foobar"
httpsPort uint = 8000
httpPort uint = 8001
clientPort uint = 8002
body = make([]byte, size)
)
_, err := rand.Read(body)
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
wg := sync.WaitGroup{}
wg.Add(2)
serverOpts := ServerOptions{
ListeningAddr: "0.0.0.0",
ListeningTLSPort: httpsPort,
ListeningHTTPPort: httpPort,
ListeningForClientsPort: clientPort,
}
s := NewServer(serverOpts, nil, map[string]Service{
domain: {ClientSecret: secret},
})
// start fake local app so client can connect to it
localApp := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(body)
}))
// start tcprouter server
go func() {
defer wg.Done()
err = s.Start(ctx)
require.NoError(t, err)
}()
// start tcprouter client
u, err := url.Parse(localApp.URL)
require.NoError(t, err)
go func() {
defer wg.Done()
local := u.Host
remote := fmt.Sprintf("%s:%d", domain, clientPort)
log.Printf("start client local:%v remote:%v\n", local, remote)
client := NewClient(secret, local, local, remote)
client.Start(ctx)
}()
// let everything settle
time.Sleep(time.Second)
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s:%d", domain, httpPort), nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
if resp.StatusCode != http.StatusOK {
t.Error("wrong status code received")
}
got, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
require.NoError(t, err)
assert.Equal(t, got, body)
localApp.Close()
cancel()
wg.Wait()
}