(t *testing.T)
| 28 | ) |
| 29 | |
| 30 | func TestHTTPVarReplacement(t *testing.T) { |
| 31 | req, _ := http.NewRequest(http.MethodGet, "/foo/bar.tar.gz?a=1&b=2", nil) |
| 32 | repl := caddy.NewReplacer() |
| 33 | localAddr, _ := net.ResolveTCPAddr("tcp", "192.168.159.1:80") |
| 34 | ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl) |
| 35 | ctx = context.WithValue(ctx, http.LocalAddrContextKey, localAddr) |
| 36 | req = req.WithContext(ctx) |
| 37 | req.Host = "example.com:80" |
| 38 | req.RemoteAddr = "192.168.159.32:1234" |
| 39 | |
| 40 | clientCert := []byte(`-----BEGIN CERTIFICATE----- |
| 41 | MIIB9jCCAV+gAwIBAgIBAjANBgkqhkiG9w0BAQsFADAYMRYwFAYDVQQDDA1DYWRk |
| 42 | eSBUZXN0IENBMB4XDTE4MDcyNDIxMzUwNVoXDTI4MDcyMTIxMzUwNVowHTEbMBkG |
| 43 | A1UEAwwSY2xpZW50LmxvY2FsZG9tYWluMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB |
| 44 | iQKBgQDFDEpzF0ew68teT3xDzcUxVFaTII+jXH1ftHXxxP4BEYBU4q90qzeKFneF |
| 45 | z83I0nC0WAQ45ZwHfhLMYHFzHPdxr6+jkvKPASf0J2v2HDJuTM1bHBbik5Ls5eq+ |
| 46 | fVZDP8o/VHKSBKxNs8Goc2NTsr5b07QTIpkRStQK+RJALk4x9QIDAQABo0swSTAJ |
| 47 | BgNVHRMEAjAAMAsGA1UdDwQEAwIHgDAaBgNVHREEEzARgglsb2NhbGhvc3SHBH8A |
| 48 | AAEwEwYDVR0lBAwwCgYIKwYBBQUHAwIwDQYJKoZIhvcNAQELBQADgYEANSjz2Sk+ |
| 49 | eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV |
| 50 | 3Q9fgDkiUod+uIK0IynzIKvw+Cjg+3nx6NQ0IM0zo8c7v398RzB4apbXKZyeeqUH |
| 51 | 9fNwfEi+OoXR6s+upSKobCmLGLGi9Na5s5g= |
| 52 | -----END CERTIFICATE-----`) |
| 53 | |
| 54 | block, _ := pem.Decode(clientCert) |
| 55 | if block == nil { |
| 56 | t.Fatalf("failed to decode PEM certificate") |
| 57 | } |
| 58 | |
| 59 | cert, err := x509.ParseCertificate(block.Bytes) |
| 60 | if err != nil { |
| 61 | t.Fatalf("failed to decode PEM certificate: %v", err) |
| 62 | } |
| 63 | |
| 64 | req.TLS = &tls.ConnectionState{ |
| 65 | Version: tls.VersionTLS13, |
| 66 | HandshakeComplete: true, |
| 67 | ServerName: "example.com", |
| 68 | CipherSuite: tls.TLS_AES_256_GCM_SHA384, |
| 69 | PeerCertificates: []*x509.Certificate{cert}, |
| 70 | NegotiatedProtocol: "h2", |
| 71 | NegotiatedProtocolIsMutual: true, |
| 72 | } |
| 73 | |
| 74 | res := httptest.NewRecorder() |
| 75 | addHTTPVarsToReplacer(repl, req, res) |
| 76 | |
| 77 | for i, tc := range []struct { |
| 78 | get string |
| 79 | expect string |
| 80 | }{ |
| 81 | { |
| 82 | get: "http.request.scheme", |
| 83 | expect: "https", |
| 84 | }, |
| 85 | { |
| 86 | get: "http.request.method", |
| 87 | expect: http.MethodGet, |
nothing calls this directly
no test coverage detected