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
| 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 integration |
| 16 | |
| 17 | import ( |
| 18 | "bufio" |
| 19 | "bytes" |
| 20 | "encoding/json" |
| 21 | "net/http" |
| 22 | "os" |
| 23 | "path/filepath" |
| 24 | "testing" |
| 25 | "time" |
| 26 | |
| 27 | "github.com/caddyserver/caddy/v2/caddytest" |
| 28 | ) |
| 29 | |
| 30 | // TestLoggerNamesCaseInsensitive loads a config whose logger_names key is |
| 31 | // mixed-case and asserts that a request with a differently-cased Host is |
| 32 | // written by the configured named logger. |
| 33 | func TestLoggerNamesCaseInsensitive(t *testing.T) { |
| 34 | logFile := filepath.Join(t.TempDir(), "access.log") |
| 35 | logFileJSON, err := json.Marshal(logFile) |
| 36 | if err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | |
| 40 | tester := caddytest.NewTester(t) |
| 41 | // Load a config without the file logger on cleanup so the log file is |
| 42 | // closed before t.TempDir removes it; Windows cant delete open files. |
| 43 | t.Cleanup(func() { |
| 44 | tester.InitServer(`{"admin": {"listen": "localhost:2999"}}`, "json") |
| 45 | }) |
| 46 | // The config is JSON because the Caddyfile adapter already lowercases |
| 47 | // site addresses, so it would never produce a mixed-case key. |
| 48 | tester.InitServer(`{ |
| 49 | "admin": {"listen": "localhost:2999"}, |
| 50 | "logging": { |
| 51 | "logs": { |
| 52 | "mixed": { |
| 53 | "writer": {"output": "file", "filename": `+string(logFileJSON)+`}, |
| 54 | "encoder": {"format": "json"}, |
| 55 | "include": ["http.log.access.mixed"] |
| 56 | } |
| 57 | } |
| 58 | }, |
| 59 | "apps": { |
| 60 | "http": { |
| 61 | "http_port": 9080, |
| 62 | "grace_period": 1, |
| 63 | "servers": { |
| 64 | "srv0": { |
| 65 | "listen": [":9080"], |
| 66 | "routes": [ |
| 67 | {"handle": [{"handler": "static_response", "body": "ok"}]} |
| 68 | ], |
| 69 | "logs": { |
| 70 | "logger_names": {"Mixed.Example.org": ["mixed"]} |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | }`, "json") |
| 77 | |
| 78 | req, err := http.NewRequest(http.MethodGet, "http://localhost:9080/", nil) |
| 79 | if err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | req.Host = "MIXED.example.org" |
| 83 | tester.AssertResponse(req, http.StatusOK, "ok") |
| 84 | |
| 85 | // the access log entry is written after the response, so poll for it |
| 86 | var logs []byte |
| 87 | for deadline := time.Now().Add(5 * time.Second); time.Now().Before(deadline); time.Sleep(50 * time.Millisecond) { |
| 88 | logs, err = os.ReadFile(logFile) |
| 89 | if err != nil { |
| 90 | continue |
| 91 | } |
| 92 | scanner := bufio.NewScanner(bytes.NewReader(logs)) |
| 93 | for scanner.Scan() { |
| 94 | var entry struct { |
| 95 | Logger string `json:"logger"` |
| 96 | Request struct { |
| 97 | Host string `json:"host"` |
| 98 | } `json:"request"` |
| 99 | } |
| 100 | if err := json.Unmarshal(scanner.Bytes(), &entry); err != nil { |
| 101 | continue |
| 102 | } |
| 103 | if entry.Logger == "http.log.access.mixed" && entry.Request.Host == "MIXED.example.org" { |
| 104 | return |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | t.Errorf("no access log entry from logger %q for host %q, got %q", "http.log.access.mixed", "MIXED.example.org", logs) |
| 109 | } |