-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathlearn_test.go
53 lines (42 loc) · 1.34 KB
/
learn_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
package beyond
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"inet.af/tcpproxy"
)
var (
proxy tcpproxy.Proxy
)
func init() {
proxy.AddRoute("127.0.0.1:9443", tcpproxy.To("1.1.1.1:443"))
}
func TestLearnProxy(t *testing.T) {
tlsConfig.InsecureSkipVerify = true
assert.NoError(t, proxy.Start())
}
func TestLearnHostScheme(t *testing.T) {
assert.Equal(t, "https://localhost:9443", learnBase("localhost"))
ports1 := *learnHTTPSPorts
ports2 := *learnHTTPPorts
learnTest1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
learnTest2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
*learnHTTPSPorts = strings.Split(learnTest1.URL, ":")[2]
*learnHTTPPorts = strings.Split(learnTest2.URL, ":")[2]
base1 := learnBase("127.0.0.1")
learnTest1.Close()
base2 := learnBase("127.0.0.1")
learnTest2.Close()
assert.Equal(t, learnTest2.URL, base1)
assert.Equal(t, learnTest2.URL, base2)
*learnHTTPSPorts = ""
*learnHTTPPorts = "80"
assert.Equal(t, "http://neverssl.com", learnBase("neverssl.com"))
*learnHTTPSPorts = ports1
*learnHTTPPorts = ports2
assert.Equal(t, "https://golang.org", learnBase("golang.org"))
assert.NotNil(t, learn("golang.org"))
assert.Equal(t, "https://golang.org:443", learnBase("golang.org:443"))
}