@@ -27,6 +27,62 @@ import (
2727 "github.com/caddyserver/caddy/v2"
2828)
2929
30+ func TestMissingCookiePlaceholder (t * testing.T ) {
31+ req := httptest .NewRequest (http .MethodGet , "/" , nil )
32+ req = req .WithContext (context .WithValue (req .Context (), VarsCtxKey , map [string ]any {}))
33+ repl := NewTestReplacer (req )
34+ const input = "before-{http.request.cookie.session}-after"
35+ if got := repl .ReplaceKnown (input , "" ); got != "before--after" {
36+ t .Fatalf ("missing cookie = %q, want %q" , got , "before--after" )
37+ }
38+ req .AddCookie (& http.Cookie {Name : "session" , Value : "present" })
39+ if got := repl .ReplaceKnown (input , "" ); got != "before-present-after" {
40+ t .Fatalf ("present cookie = %q, want %q" , got , "before-present-after" )
41+ }
42+ }
43+
44+ func TestTLSPlaceholdersWithoutTLS (t * testing.T ) {
45+ req := httptest .NewRequest (http .MethodGet , "/" , nil )
46+ req = req .WithContext (context .WithValue (req .Context (), VarsCtxKey , map [string ]any {}))
47+ repl := NewTestReplacer (req )
48+ for _ , field := range []string {
49+ "version" , "cipher_suite" , "resumed" , "proto" , "proto_mutual" , "server_name" , "ech" ,
50+ "client.fingerprint" , "client.public_key" , "client.public_key_sha256" ,
51+ "client.issuer" , "client.serial" , "client.subject" , "client.certificate_pem" , "client.certificate_der_base64" ,
52+ "client.san.dns_names" , "client.san.emails" , "client.san.ips" , "client.san.uris" ,
53+ "client.san.dns_names.0" , "client.san.emails.0" , "client.san.ips.0" , "client.san.uris.0" ,
54+ } {
55+ t .Run (field , func (t * testing.T ) {
56+ key := "http.request.tls." + field
57+ value , known := repl .Get (key )
58+ if ! known || caddy .ToString (value ) != "" {
59+ t .Fatalf ("Get(%q) = %v, %v; want empty, known" , key , value , known )
60+ }
61+ if got := repl .ReplaceKnown ("before-{" + key + "}-after" , "" ); got != "before--after" {
62+ t .Fatalf ("replacement = %q, want %q" , got , "before--after" )
63+ }
64+ })
65+ }
66+ }
67+
68+ func TestUnknownTLSPlaceholdersRemainLiteral (t * testing.T ) {
69+ for _ , state := range []* tls.ConnectionState {nil , {ServerName : "example.com" }} {
70+ req := httptest .NewRequest (http .MethodGet , "/" , nil )
71+ req .TLS = state
72+ req = req .WithContext (context .WithValue (req .Context (), VarsCtxKey , map [string ]any {}))
73+ repl := NewTestReplacer (req )
74+ for _ , field := range []string {
75+ "unknown" , "client.unknown" , "client.san.unknown" ,
76+ "client.san.dns_names_extra" , "client.san.dns_names.-1" , "client.san.dns_names.nope" ,
77+ } {
78+ input := "{http.request.tls." + field + "}"
79+ if got := repl .ReplaceKnown (input , "" ); got != input {
80+ t .Errorf ("replacement = %q, want literal %q" , got , input )
81+ }
82+ }
83+ }
84+ }
85+
3086func TestHTTPVarReplacement (t * testing.T ) {
3187 req , _ := http .NewRequest (http .MethodGet , "/foo/bar.tar.gz?a=1&b=2" , nil )
3288 repl := caddy .NewReplacer ()
@@ -334,4 +390,3 @@ func TestHTTPVarReplacementUUID(t *testing.T) {
334390 t .Errorf ("expected stable uuid across references: %q != %q" , first , second )
335391 }
336392}
337-
0 commit comments