|
1 | 1 | package httpcaddyfile |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" |
| 8 | + "github.com/caddyserver/caddy/v2/modules/caddyhttp" |
7 | 9 | ) |
8 | 10 |
|
9 | 11 | func TestMatcherSyntax(t *testing.T) { |
@@ -209,3 +211,54 @@ func TestGlobalOptions(t *testing.T) { |
209 | 211 | } |
210 | 212 | } |
211 | 213 | } |
| 214 | + |
| 215 | +func TestDefaultSNIWithoutHTTPS(t *testing.T) { |
| 216 | + caddyfileStr := `{ |
| 217 | + default_sni my-sni.com |
| 218 | + } |
| 219 | + example.com { |
| 220 | + file_server |
| 221 | + }` |
| 222 | + |
| 223 | + adapter := caddyfile.Adapter{ |
| 224 | + ServerType: ServerType{}, |
| 225 | + } |
| 226 | + |
| 227 | + result, _, err := adapter.Adapt([]byte(caddyfileStr), nil) |
| 228 | + if err != nil { |
| 229 | + t.Fatalf("Failed to adapt Caddyfile: %v", err) |
| 230 | + } |
| 231 | + |
| 232 | + var config struct { |
| 233 | + Apps struct { |
| 234 | + HTTP struct { |
| 235 | + Servers map[string]*caddyhttp.Server `json:"servers"` |
| 236 | + } `json:"http"` |
| 237 | + } `json:"apps"` |
| 238 | + } |
| 239 | + |
| 240 | + if err := json.Unmarshal(result, &config); err != nil { |
| 241 | + t.Fatalf("Failed to unmarshal JSON config: %v", err) |
| 242 | + } |
| 243 | + |
| 244 | + server, ok := config.Apps.HTTP.Servers["srv0"] |
| 245 | + if !ok { |
| 246 | + t.Fatalf("Expected server 'srv0' to be created") |
| 247 | + } |
| 248 | + |
| 249 | + if len(server.TLSConnPolicies) == 0 { |
| 250 | + t.Fatalf("Expected TLS connection policies to be generated, got none") |
| 251 | + } |
| 252 | + |
| 253 | + found := false |
| 254 | + for _, policy := range server.TLSConnPolicies { |
| 255 | + if policy.DefaultSNI == "my-sni.com" { |
| 256 | + found = true |
| 257 | + break |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + if !found { |
| 262 | + t.Errorf("Expected default_sni 'my-sni.com' in TLS connection policies, but it was missing. Generated JSON: %s", string(result)) |
| 263 | + } |
| 264 | +} |
0 commit comments