File
Blob: tun/client/proxy_tls_test.go
| 1 | package client |
| 2 | |
| 3 | import ( |
| 4 | "crypto/x509" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "net/url" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/stretchr/testify/require" |
| 12 | "go.uber.org/zap/zaptest" |
| 13 | ) |
| 14 | |
| 15 | func TestHTTPProxyHTTPSWithPort(t *testing.T) { |
| 16 | backend := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 17 | fmt.Fprint(w, r.Host) |
| 18 | })) |
| 19 | defer backend.Close() |
| 20 | roots := x509.NewCertPool() |
| 21 | roots.AddCert(backend.Certificate()) |
| 22 | |
| 23 | u, err := url.Parse(backend.URL) |
| 24 | require.NoError(t, err) |
| 25 | f := newForwarder(zaptest.NewLogger(t)) |
| 26 | proxy := f.newHTTPProxy(testHostname, Tunnel{parsed: u}) |
| 27 | defer proxy.close() |
| 28 | proxy.transport.TLSClientConfig.RootCAs = roots |
| 29 | req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "http://example/", nil) |
| 30 | result := httptest.NewRecorder() |
| 31 | proxy.forwarder.Handler.ServeHTTP(result, req) |
| 32 | |
| 33 | require.Equal(t, http.StatusOK, result.Code, result.Body.String()) |
| 34 | require.Equal(t, u.Host, result.Body.String()) |
| 35 | } |