@marcel91200 / caddy Mirror · read-only
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 11, 01:00 UTC

master
go 96 lines 2.44 KB
Raw
1 // Copyright 2015 Matthew Holt and The Caddy Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package caddytls
16
17 import (
18 "encoding/json"
19 "testing"
20
21 "github.com/caddyserver/caddy/v2"
22 )
23
24 func TestAvoidDuplicateAutomation(t *testing.T) {
25 tests := []struct {
26 name string
27 automateNames []string
28 expectedToManage bool
29 }{
30 {
31 name: "do not manage if wildcard is automated",
32 automateNames: []string{"*.example.com"},
33 expectedToManage: false,
34 },
35 {
36 name: "manage if no automation configured",
37 automateNames: []string{},
38 expectedToManage: true,
39 },
40 {
41 name: "manage if explicitly requested even when wildcard automated",
42 automateNames: []string{"*.example.com", "sub.example.com"},
43 expectedToManage: true,
44 },
45 }
46
47 for _, tc := range tests {
48 t.Run(tc.name, func(t *testing.T) {
49 automateJSON, err := json.Marshal(tc.automateNames)
50 if err != nil {
51 t.Fatal(err)
52 }
53
54 tlsApp := &TLS{
55 Automation: &AutomationConfig{
56 Policies: []*AutomationPolicy{
57 {
58 IssuersRaw: []json.RawMessage{
59 []byte(`{"module": "internal"}`),
60 },
61 },
62 },
63 },
64 CertificatesRaw: map[string]json.RawMessage{
65 "automate": automateJSON,
66 },
67 }
68
69 var cfg caddy.Config
70 ctx, err := caddy.ProvisionContext(&cfg)
71 if err != nil {
72 t.Fatal(err)
73 }
74
75 if err := tlsApp.Provision(ctx); err != nil {
76 t.Fatal(err)
77 }
78
79 // simulate a case wherein the HTTP app starts first and
80 // tells the TLS app about the following auto-HTTPS domains
81 httpDomains := map[string]struct{}{"sub.example.com": {}}
82 if err := tlsApp.Manage(httpDomains); err != nil {
83 t.Fatal(err)
84 }
85
86 _, actuallyManaged := tlsApp.managing["sub.example.com"]
87 if actuallyManaged != tc.expectedToManage {
88 t.Errorf(
89 "expected sub.example.com individually managed: %v, got: %v",
90 tc.expectedToManage,
91 actuallyManaged,
92 )
93 }
94 })
95 }
96 }