-
Notifications
You must be signed in to change notification settings - Fork 232
/
httpclient_test.go
110 lines (98 loc) · 3.39 KB
/
httpclient_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
// tests ./httpclient/ but is in root as it needs access to test files in root
package forwardproxy
import (
"crypto/tls"
"fmt"
"net"
"sync"
"testing"
"time"
"github.com/caddyserver/forwardproxy/httpclient"
)
func TestHttpClient(t *testing.T) {
_test := func(urlSchemeAndCreds, urlAddress string) {
for _, httpProxyVer := range testHTTPProxyVersions {
for _, httpTargetVer := range testHTTPTargetVersions {
for _, resource := range testResources {
// always dial localhost for testing purposes
proxyURL := fmt.Sprintf("%s@%s", urlSchemeAndCreds, urlAddress)
dialer, err := httpclient.NewHTTPConnectDialer(proxyURL)
if err != nil {
t.Fatal(err)
}
dialer.DialTLS = func(network string, address string) (net.Conn, string, error) {
// always dial localhost for testing purposes
conn, err := tls.Dial(network, address, &tls.Config{
InsecureSkipVerify: true,
NextProtos: []string{httpVersionToALPN[httpProxyVer]},
})
if err != nil {
return nil, "", err
}
return conn, conn.ConnectionState().NegotiatedProtocol, nil
}
// always dial localhost for testing purposes
conn, err := dialer.Dial("tcp", caddyTestTarget.addr)
if err != nil {
t.Fatal(err)
}
response, err := getResourceViaProxyConn(conn, caddyTestTarget.addr, resource, httpTargetVer, credentialsCorrect)
if err != nil {
t.Fatal(httpProxyVer, httpTargetVer, err)
} else if err = responseExpected(response, caddyTestTarget.contents[resource]); err != nil {
t.Fatal(httpProxyVer, httpTargetVer, err)
}
}
}
}
}
_test("https://"+credentialsCorrectPlain, caddyForwardProxyAuth.addr)
_test("http://"+credentialsCorrectPlain, caddyHTTPForwardProxyAuth.addr)
}
func TestHttpClientH2Multiplexing(t *testing.T) {
// doesn't actually confirm that it is multiplexed, just that it doesn't break things
// but it was manually inspected in Wireshark when this code was committed
httpProxyVer := "HTTP/2.0"
httpTargetVer := "HTTP/1.1"
dialer, err := httpclient.NewHTTPConnectDialer("https://" + credentialsCorrectPlain + "@" + caddyForwardProxyAuth.addr)
if err != nil {
t.Fatal(err)
}
dialer.DialTLS = func(network string, address string) (net.Conn, string, error) {
// always dial localhost for testing purposes
conn, err := tls.Dial(network, address, &tls.Config{
InsecureSkipVerify: true,
NextProtos: []string{httpVersionToALPN[httpProxyVer]},
})
if err != nil {
return nil, "", err
}
return conn, conn.ConnectionState().NegotiatedProtocol, nil
}
retries := 20
sleepInterval := time.Millisecond * 100
var wg sync.WaitGroup
wg.Add(retries + 1) // + for one serial launch
_test := func() {
defer wg.Done()
for _, resource := range testResources {
// always dial localhost for testing purposes
conn, err := dialer.Dial("tcp", caddyTestTarget.addr)
if err != nil {
t.Fatal(err)
}
response, err := getResourceViaProxyConn(conn, caddyTestTarget.addr, resource, httpTargetVer, credentialsCorrect)
if err != nil {
t.Fatal(httpProxyVer, httpTargetVer, err)
} else if err = responseExpected(response, caddyTestTarget.contents[resource]); err != nil {
t.Fatal(httpProxyVer, httpTargetVer, err)
}
}
}
_test() // do serially at least once
for i := 0; i < retries; i++ {
// nolint:govet // this is a test
go _test()
time.Sleep(sleepInterval)
}
}