-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_test.go
More file actions
151 lines (138 loc) · 3.99 KB
/
Copy pathproxy_test.go
File metadata and controls
151 lines (138 loc) · 3.99 KB
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
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"io"
"math/big"
"net"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
)
func TestProxy_RoundTrip(t *testing.T) {
// 1. Dummy upstream on a random port.
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello from %s", r.URL.Path)
}))
defer upstream.Close()
upURL, _ := url.Parse(upstream.URL)
upPort, _ := strconv.Atoi(upURL.Port())
// 2. Generate self-signed cert to a temp dir.
dir := t.TempDir()
certPath := filepath.Join(dir, "cert.pem")
keyPath := filepath.Join(dir, "key.pem")
writeSelfSigned(t, certPath, keyPath)
cert, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
t.Fatal(err)
}
// 3. Start TLS proxy on a random port.
ln, err := tls.Listen("tcp", "127.0.0.1:0", &tls.Config{
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS12,
})
if err != nil {
t.Fatal(err)
}
events := make(chan Event, 8)
srv := &http.Server{Handler: newProxyHandler(upPort, events)}
go srv.Serve(ln)
defer srv.Close()
// 4. HTTPS client with skip-verify (self-signed).
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Timeout: 5 * time.Second,
}
// 5. Round-trip.
resp, err := client.Get("https://" + ln.Addr().String() + "/greet")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatalf("status = %d, want 200", resp.StatusCode)
}
body, _ := io.ReadAll(resp.Body)
if !strings.Contains(string(body), "hello from /greet") {
t.Fatalf("body = %q, want it to contain 'hello from /greet'", body)
}
// 6. Event was published.
select {
case ev := <-events:
if ev.Method != "GET" || ev.Path != "/greet" || ev.Status != 200 {
t.Fatalf("bad event: %+v", ev)
}
case <-time.After(time.Second):
t.Fatal("timed out waiting for event")
}
}
func TestProxy_UpstreamDown_Returns502(t *testing.T) {
// Find a definitely-free port by opening then closing a listener.
l, _ := net.Listen("tcp", "127.0.0.1:0")
deadPort := l.Addr().(*net.TCPAddr).Port
l.Close()
dir := t.TempDir()
certPath := filepath.Join(dir, "cert.pem")
keyPath := filepath.Join(dir, "key.pem")
writeSelfSigned(t, certPath, keyPath)
cert, _ := tls.LoadX509KeyPair(certPath, keyPath)
ln, _ := tls.Listen("tcp", "127.0.0.1:0", &tls.Config{
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS12,
})
srv := &http.Server{Handler: newProxyHandler(deadPort, nil)}
go srv.Serve(ln)
defer srv.Close()
client := &http.Client{
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
Timeout: 3 * time.Second,
}
resp, err := client.Get("https://" + ln.Addr().String() + "/")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusBadGateway {
t.Fatalf("status = %d, want 502", resp.StatusCode)
}
}
func writeSelfSigned(t *testing.T, certPath, keyPath string) {
t.Helper()
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatal(err)
}
tmpl := x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "test"},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
IPAddresses: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("::1")},
DNSNames: []string{"localhost"},
}
der, err := x509.CreateCertificate(rand.Reader, &tmpl, &tmpl, &key.PublicKey, key)
if err != nil {
t.Fatal(err)
}
certOut, _ := os.Create(certPath)
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: der})
certOut.Close()
keyOut, _ := os.Create(keyPath)
pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
keyOut.Close()
}