| 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 templatesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/ |
| 16 | |
| 17 | import ( |
| 18 | "bytesscip-go gomod github.com/golang/go/src go1.25.1 bytes/" |
| 19 | "contextscip-go gomod github.com/golang/go/src go1.25.1 context/" |
| 20 | "errorsscip-go gomod github.com/golang/go/src go1.25.1 errors/" |
| 21 | "fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/" |
| 22 | "io/fsscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/" |
| 23 | "net/httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/" |
| 24 | "osscip-go gomod github.com/golang/go/src go1.25.1 os/" |
| 25 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/" |
| 26 | "reflectscip-go gomod github.com/golang/go/src go1.25.1 reflect/" |
| 27 | "sortscip-go gomod github.com/golang/go/src go1.25.1 sort/" |
| 28 | "stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/" |
| 29 | "testingscip-go gomod github.com/golang/go/src go1.25.1 testing/" |
| 30 | "timescip-go gomod github.com/golang/go/src go1.25.1 time/" |
| 31 | |
| 32 | "github.com/caddyserver/caddy/v2/modules/caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/" |
| 33 | ) |
| 34 | |
| 35 | type handlescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/handle# struct{} |
| 36 | |
| 37 | func (h *handlescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/handle#) ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/handle#ServeHTTP().(w httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, r *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#) { |
| 38 | if r.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Header..Getscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Get().("Accept-Encoding") == "identity" { |
| 39 | w.Writescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#Write().([]byte("good contents")) |
| 40 | } else { |
| 41 | w.Writescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#Write().([]byte("bad cause Accept-Encoding: " + r.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Header..Getscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Get().("Accept-Encoding"))) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func TestHTTPIncludescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TestHTTPInclude().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 46 | tplContext := getContextOrFailscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/getContextOrFail().(t) |
| 47 | for i, test := range []struct { |
| 48 | uri string |
| 49 | handler *handlescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/handle# |
| 50 | expect string |
| 51 | }{ |
| 52 | { |
| 53 | uri: "https://example.com/foo/bar", |
| 54 | handler: &handlescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/handle#{}, |
| 55 | expect: "good contents", |
| 56 | }, |
| 57 | } { |
| 58 | ctx := contextscip-go gomod github.com/golang/go/src go1.25.1 context/.WithValuescip-go gomod github.com/golang/go/src go1.25.1 context/WithValue().(tplContext.Reqscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#Req..Contextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Context().(), caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.ServerCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ServerCtxKey., test.handler) |
| 59 | tplContext.Reqscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#Req. = tplContext.Reqscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#Req..WithContextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#WithContext().(ctx) |
| 60 | tplContext.Reqscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#Req..Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Header..Addscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Add().("Accept-Encoding", "gzip") |
| 61 | result, err := tplContext.funcHTTPIncludescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#funcHTTPInclude().(test.uri) |
| 62 | if result != test.expect { |
| 63 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: expected '%s' but got '%s'", i, test.expect, result) |
| 64 | } |
| 65 | if err != nil { |
| 66 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: got error: %v", i, result) |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestMarkdownscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TestMarkdown().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 72 | tplContext := getContextOrFailscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/getContextOrFail().(t) |
| 73 | |
| 74 | for i, test := range []struct { |
| 75 | body string |
| 76 | expect string |
| 77 | }{ |
| 78 | { |
| 79 | body: "- str1\n- str2\n", |
| 80 | expect: "<ul>\n<li>str1</li>\n<li>str2</li>\n</ul>\n", |
| 81 | }, |
| 82 | } { |
| 83 | result, err := tplContext.funcMarkdownscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#funcMarkdown().(test.body) |
| 84 | if result != test.expect { |
| 85 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: expected '%s' but got '%s'", i, test.expect, result) |
| 86 | } |
| 87 | if err != nil { |
| 88 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: got error: %v", i, result) |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func TestCookiescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TestCookie().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 94 | for i, test := range []struct { |
| 95 | cookie *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Cookiescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Cookie# |
| 96 | cookieName string |
| 97 | expect string |
| 98 | }{ |
| 99 | { |
| 100 | // happy path |
| 101 | cookie: &httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Cookiescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Cookie#{Namescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Cookie#Name.: "cookieName", Valuescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Cookie#Value.: "cookieValue"}, |
| 102 | cookieName: "cookieName", |
| 103 | expect: "cookieValue", |
| 104 | }, |
| 105 | { |
| 106 | // try to get a non-existing cookie |
| 107 | cookie: &httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Cookiescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Cookie#{Namescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Cookie#Name.: "cookieName", Valuescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Cookie#Value.: "cookieValue"}, |
| 108 | cookieName: "notExisting", |
| 109 | expect: "", |
| 110 | }, |
| 111 | { |
| 112 | // partial name match |
| 113 | cookie: &httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Cookiescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Cookie#{Namescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Cookie#Name.: "cookie", Valuescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Cookie#Value.: "cookieValue"}, |
| 114 | cookieName: "cook", |
| 115 | expect: "", |
| 116 | }, |
| 117 | { |
| 118 | // cookie with optional fields |
| 119 | cookie: &httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Cookiescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Cookie#{Namescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Cookie#Name.: "cookie", Valuescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Cookie#Value.: "cookieValue", Pathscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Cookie#Path.: "/path", Domainscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Cookie#Domain.: "https://localhost", Expiresscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Cookie#Expires.: timescip-go gomod github.com/golang/go/src go1.25.1 time/.Nowscip-go gomod github.com/golang/go/src go1.25.1 time/Now().().Addscip-go gomod github.com/golang/go/src go1.25.1 time/Time#Add().(10 * timescip-go gomod github.com/golang/go/src go1.25.1 time/.Minutescip-go gomod github.com/golang/go/src go1.25.1 time/Minute.), MaxAgescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Cookie#MaxAge.: 120}, |
| 120 | cookieName: "cookie", |
| 121 | expect: "cookieValue", |
| 122 | }, |
| 123 | } { |
| 124 | tplContext := getContextOrFailscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/getContextOrFail().(t) |
| 125 | tplContext.Reqscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#Req..AddCookiescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#AddCookie().(test.cookie) |
| 126 | actual := tplContext.Cookiescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#Cookie().(test.cookieName) |
| 127 | if actual != test.expect { |
| 128 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected cookie value '%s' but got '%s' for cookie with name '%s'", |
| 129 | i, test.expect, actual, test.cookieName) |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | func TestImportscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TestImport().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 135 | for i, test := range []struct { |
| 136 | fileContent string |
| 137 | fileName string |
| 138 | shouldErr bool |
| 139 | expect string |
| 140 | }{ |
| 141 | { |
| 142 | // file exists, template is defined |
| 143 | fileContent: `{{ define "imported" }}text{{end}}`, |
| 144 | fileName: "file1", |
| 145 | shouldErr: false, |
| 146 | expect: `"imported"`, |
| 147 | }, |
| 148 | { |
| 149 | // file does not exit |
| 150 | fileContent: "", |
| 151 | fileName: "", |
| 152 | shouldErr: true, |
| 153 | }, |
| 154 | } { |
| 155 | tplContext := getContextOrFailscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/getContextOrFail().(t) |
| 156 | var absFilePath string |
| 157 | |
| 158 | // create files for test case |
| 159 | if test.fileName != "" { |
| 160 | absFilePath := filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/Join().(fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Sprintf().("%s", tplContext.Rootscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#Root.), test.fileName) |
| 161 | if err := osscip-go gomod github.com/golang/go/src go1.25.1 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.1 os/WriteFile().(absFilePath, []byte(test.fileContent), osscip-go gomod github.com/golang/go/src go1.25.1 os/.ModePermscip-go gomod github.com/golang/go/src go1.25.1 os/ModePerm.); err != nil { |
| 162 | osscip-go gomod github.com/golang/go/src go1.25.1 os/.Removescip-go gomod github.com/golang/go/src go1.25.1 os/Remove().(absFilePath) |
| 163 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Test %d: Expected no error creating file, got: '%s'", i, err.Error()) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // perform test |
| 168 | tplContext.NewTemplatescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#NewTemplate().("parent") |
| 169 | actual, err := tplContext.funcImportscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#funcImport().(test.fileName) |
| 170 | templateWasDefined := stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.1 strings/Contains().(tplContext.tplscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#tpl..DefinedTemplatesscip-go gomod github.com/golang/go/src go1.25.1 `text/template`/Template#DefinedTemplates().(), test.expect) |
| 171 | if err != nil { |
| 172 | if !test.shouldErr { |
| 173 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected no error, got: '%s'", i, err) |
| 174 | } |
| 175 | } else if test.shouldErr { |
| 176 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected error but had none", i) |
| 177 | } else if !templateWasDefined && actual != "" { |
| 178 | // template should be defined, return value should be an empty string |
| 179 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected template %s to be define but got %s", i, test.expect, tplContext.tplscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#tpl..DefinedTemplatesscip-go gomod github.com/golang/go/src go1.25.1 `text/template`/Template#DefinedTemplates().()) |
| 180 | } |
| 181 | |
| 182 | if absFilePath != "" { |
| 183 | if err := osscip-go gomod github.com/golang/go/src go1.25.1 os/.Removescip-go gomod github.com/golang/go/src go1.25.1 os/Remove().(absFilePath); err != nil && !errorsscip-go gomod github.com/golang/go/src go1.25.1 errors/.Isscip-go gomod github.com/golang/go/src go1.25.1 errors/Is().(err, fsscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/.ErrNotExistscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/ErrNotExist.) { |
| 184 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Test %d: Expected no error removing temporary test file, got: %v", i, err) |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | func TestNestedIncludescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TestNestedInclude().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 191 | for i, test := range []struct { |
| 192 | child string |
| 193 | childFile string |
| 194 | parent string |
| 195 | parentFile string |
| 196 | shouldErr bool |
| 197 | expect string |
| 198 | child2 string |
| 199 | child2File string |
| 200 | }{ |
| 201 | { |
| 202 | // include in parent |
| 203 | child: `{{ include "file1" }}`, |
| 204 | childFile: "file0", |
| 205 | parent: `{{ $content := "file2" }}{{ $p := include $content}}`, |
| 206 | parentFile: "file1", |
| 207 | shouldErr: false, |
| 208 | expect: ``, |
| 209 | child2: `This shouldn't show`, |
| 210 | child2File: "file2", |
| 211 | }, |
| 212 | } { |
| 213 | context := getContextOrFailscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/getContextOrFail().(t) |
| 214 | var absFilePath string |
| 215 | var absFilePath0 string |
| 216 | var absFilePath1 string |
| 217 | var buf *bytesscip-go gomod github.com/golang/go/src go1.25.1 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.1 bytes/Buffer# |
| 218 | var err error |
| 219 | |
| 220 | // create files and for test case |
| 221 | if test.parentFile != "" { |
| 222 | absFilePath = filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/Join().(fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Sprintf().("%s", context.Rootscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#Root.), test.parentFile) |
| 223 | if err := osscip-go gomod github.com/golang/go/src go1.25.1 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.1 os/WriteFile().(absFilePath, []byte(test.parent), osscip-go gomod github.com/golang/go/src go1.25.1 os/.ModePermscip-go gomod github.com/golang/go/src go1.25.1 os/ModePerm.); err != nil { |
| 224 | osscip-go gomod github.com/golang/go/src go1.25.1 os/.Removescip-go gomod github.com/golang/go/src go1.25.1 os/Remove().(absFilePath) |
| 225 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Test %d: Expected no error creating file, got: '%s'", i, err.Error()) |
| 226 | } |
| 227 | } |
| 228 | if test.childFile != "" { |
| 229 | absFilePath0 = filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/Join().(fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Sprintf().("%s", context.Rootscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#Root.), test.childFile) |
| 230 | if err := osscip-go gomod github.com/golang/go/src go1.25.1 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.1 os/WriteFile().(absFilePath0, []byte(test.child), osscip-go gomod github.com/golang/go/src go1.25.1 os/.ModePermscip-go gomod github.com/golang/go/src go1.25.1 os/ModePerm.); err != nil { |
| 231 | osscip-go gomod github.com/golang/go/src go1.25.1 os/.Removescip-go gomod github.com/golang/go/src go1.25.1 os/Remove().(absFilePath0) |
| 232 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Test %d: Expected no error creating file, got: '%s'", i, err.Error()) |
| 233 | } |
| 234 | } |
| 235 | if test.child2File != "" { |
| 236 | absFilePath1 = filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/Join().(fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Sprintf().("%s", context.Rootscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#Root.), test.child2File) |
| 237 | if err := osscip-go gomod github.com/golang/go/src go1.25.1 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.1 os/WriteFile().(absFilePath1, []byte(test.child2), osscip-go gomod github.com/golang/go/src go1.25.1 os/.ModePermscip-go gomod github.com/golang/go/src go1.25.1 os/ModePerm.); err != nil { |
| 238 | osscip-go gomod github.com/golang/go/src go1.25.1 os/.Removescip-go gomod github.com/golang/go/src go1.25.1 os/Remove().(absFilePath0) |
| 239 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Test %d: Expected no error creating file, got: '%s'", i, err.Error()) |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | buf = bufPoolscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/bufPool..Getscip-go gomod github.com/golang/go/src go1.25.1 sync/Pool#Get().().(*bytesscip-go gomod github.com/golang/go/src go1.25.1 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.1 bytes/Buffer#) |
| 244 | buf.Resetscip-go gomod github.com/golang/go/src go1.25.1 bytes/Buffer#Reset().() |
| 245 | defer bufPoolscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/bufPool..Putscip-go gomod github.com/golang/go/src go1.25.1 sync/Pool#Put().(buf) |
| 246 | buf.WriteStringscip-go gomod github.com/golang/go/src go1.25.1 bytes/Buffer#WriteString().(test.child) |
| 247 | err = context.executeTemplateInBufferscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#executeTemplateInBuffer().(test.childFile, buf) |
| 248 | |
| 249 | if err != nil { |
| 250 | if !test.shouldErr { |
| 251 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected no error, got: '%s'", i, err) |
| 252 | } |
| 253 | } else if test.shouldErr { |
| 254 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected error but had none", i) |
| 255 | } else if buf.Stringscip-go gomod github.com/golang/go/src go1.25.1 bytes/Buffer#String().() != test.expect { |
| 256 | // |
| 257 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected '%s' but got '%s'", i, test.expect, buf.Stringscip-go gomod github.com/golang/go/src go1.25.1 bytes/Buffer#String().()) |
| 258 | } |
| 259 | |
| 260 | if absFilePath != "" { |
| 261 | if err := osscip-go gomod github.com/golang/go/src go1.25.1 os/.Removescip-go gomod github.com/golang/go/src go1.25.1 os/Remove().(absFilePath); err != nil && !errorsscip-go gomod github.com/golang/go/src go1.25.1 errors/.Isscip-go gomod github.com/golang/go/src go1.25.1 errors/Is().(err, fsscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/.ErrNotExistscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/ErrNotExist.) { |
| 262 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Test %d: Expected no error removing temporary test file, got: %v", i, err) |
| 263 | } |
| 264 | } |
| 265 | if absFilePath0 != "" { |
| 266 | if err := osscip-go gomod github.com/golang/go/src go1.25.1 os/.Removescip-go gomod github.com/golang/go/src go1.25.1 os/Remove().(absFilePath0); err != nil && !errorsscip-go gomod github.com/golang/go/src go1.25.1 errors/.Isscip-go gomod github.com/golang/go/src go1.25.1 errors/Is().(err, fsscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/.ErrNotExistscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/ErrNotExist.) { |
| 267 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Test %d: Expected no error removing temporary test file, got: %v", i, err) |
| 268 | } |
| 269 | } |
| 270 | if absFilePath1 != "" { |
| 271 | if err := osscip-go gomod github.com/golang/go/src go1.25.1 os/.Removescip-go gomod github.com/golang/go/src go1.25.1 os/Remove().(absFilePath1); err != nil && !errorsscip-go gomod github.com/golang/go/src go1.25.1 errors/.Isscip-go gomod github.com/golang/go/src go1.25.1 errors/Is().(err, fsscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/.ErrNotExistscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/ErrNotExist.) { |
| 272 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Test %d: Expected no error removing temporary test file, got: %v", i, err) |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | func TestIncludescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TestInclude().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 279 | for i, test := range []struct { |
| 280 | fileContent string |
| 281 | fileName string |
| 282 | shouldErr bool |
| 283 | expect string |
| 284 | args string |
| 285 | }{ |
| 286 | { |
| 287 | // file exists, content is text only |
| 288 | fileContent: "text", |
| 289 | fileName: "file1", |
| 290 | shouldErr: false, |
| 291 | expect: "text", |
| 292 | }, |
| 293 | { |
| 294 | // file exists, content is template |
| 295 | fileContent: "{{ if . }}text{{ end }}", |
| 296 | fileName: "file1", |
| 297 | shouldErr: false, |
| 298 | expect: "text", |
| 299 | }, |
| 300 | { |
| 301 | // file does not exit |
| 302 | fileContent: "", |
| 303 | fileName: "", |
| 304 | shouldErr: true, |
| 305 | }, |
| 306 | { |
| 307 | // args |
| 308 | fileContent: "{{ index .Args 0 }}", |
| 309 | fileName: "file1", |
| 310 | shouldErr: false, |
| 311 | args: "text", |
| 312 | expect: "text", |
| 313 | }, |
| 314 | { |
| 315 | // args, reference arg out of range |
| 316 | fileContent: "{{ index .Args 1 }}", |
| 317 | fileName: "file1", |
| 318 | shouldErr: true, |
| 319 | args: "text", |
| 320 | }, |
| 321 | } { |
| 322 | tplContext := getContextOrFailscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/getContextOrFail().(t) |
| 323 | var absFilePath string |
| 324 | |
| 325 | // create files for test case |
| 326 | if test.fileName != "" { |
| 327 | absFilePath := filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/Join().(fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Sprintf().("%s", tplContext.Rootscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#Root.), test.fileName) |
| 328 | if err := osscip-go gomod github.com/golang/go/src go1.25.1 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.1 os/WriteFile().(absFilePath, []byte(test.fileContent), osscip-go gomod github.com/golang/go/src go1.25.1 os/.ModePermscip-go gomod github.com/golang/go/src go1.25.1 os/ModePerm.); err != nil { |
| 329 | osscip-go gomod github.com/golang/go/src go1.25.1 os/.Removescip-go gomod github.com/golang/go/src go1.25.1 os/Remove().(absFilePath) |
| 330 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Test %d: Expected no error creating file, got: '%s'", i, err.Error()) |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // perform test |
| 335 | actual, err := tplContext.funcIncludescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#funcInclude().(test.fileName, test.args) |
| 336 | if err != nil { |
| 337 | if !test.shouldErr { |
| 338 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected no error, got: '%s'", i, err) |
| 339 | } |
| 340 | } else if test.shouldErr { |
| 341 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected error but had none", i) |
| 342 | } else if actual != test.expect { |
| 343 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected %s but got %s", i, test.expect, actual) |
| 344 | } |
| 345 | |
| 346 | if absFilePath != "" { |
| 347 | if err := osscip-go gomod github.com/golang/go/src go1.25.1 os/.Removescip-go gomod github.com/golang/go/src go1.25.1 os/Remove().(absFilePath); err != nil && !errorsscip-go gomod github.com/golang/go/src go1.25.1 errors/.Isscip-go gomod github.com/golang/go/src go1.25.1 errors/Is().(err, fsscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/.ErrNotExistscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/ErrNotExist.) { |
| 348 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Test %d: Expected no error removing temporary test file, got: %v", i, err) |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | func TestCookieMultipleCookiesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TestCookieMultipleCookies().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 355 | tplContext := getContextOrFailscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/getContextOrFail().(t) |
| 356 | |
| 357 | cookieNameBase, cookieValueBase := "cookieName", "cookieValue" |
| 358 | |
| 359 | for i := 0; i < 10; i++ { |
| 360 | tplContext.Reqscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#Req..AddCookiescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#AddCookie().(&httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Cookiescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Cookie#{ |
| 361 | Namescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Cookie#Name.: fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Sprintf().("%s%d", cookieNameBase, i), |
| 362 | Valuescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Cookie#Value.: fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Sprintf().("%s%d", cookieValueBase, i), |
| 363 | }) |
| 364 | } |
| 365 | |
| 366 | for i := 0; i < 10; i++ { |
| 367 | expectedCookieVal := fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Sprintf().("%s%d", cookieValueBase, i) |
| 368 | actualCookieVal := tplContext.Cookiescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#Cookie().(fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Sprintf().("%s%d", cookieNameBase, i)) |
| 369 | if actualCookieVal != expectedCookieVal { |
| 370 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Expected cookie value %s, found %s", expectedCookieVal, actualCookieVal) |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | func TestIPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TestIP().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 376 | tplContext := getContextOrFailscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/getContextOrFail().(t) |
| 377 | for i, test := range []struct { |
| 378 | inputRemoteAddr string |
| 379 | expect string |
| 380 | }{ |
| 381 | {"1.1.1.1:1111", "1.1.1.1"}, |
| 382 | {"1.1.1.1", "1.1.1.1"}, |
| 383 | {"[::1]:11", "::1"}, |
| 384 | {"[2001:db8:a0b:12f0::1]", "[2001:db8:a0b:12f0::1]"}, |
| 385 | {`[fe80:1::3%eth0]:44`, `fe80:1::3%eth0`}, |
| 386 | } { |
| 387 | tplContext.Reqscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#Req..RemoteAddrscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#RemoteAddr. = test.inputRemoteAddr |
| 388 | if actual := tplContext.RemoteIPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#RemoteIP().(); actual != test.expect { |
| 389 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected %s but got %s", i, test.expect, actual) |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | func TestStripHTMLscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TestStripHTML().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 395 | tplContext := getContextOrFailscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/getContextOrFail().(t) |
| 396 | |
| 397 | for i, test := range []struct { |
| 398 | input string |
| 399 | expect string |
| 400 | }{ |
| 401 | { |
| 402 | // no tags |
| 403 | input: `h1`, |
| 404 | expect: `h1`, |
| 405 | }, |
| 406 | { |
| 407 | // happy path |
| 408 | input: `<h1>h1</h1>`, |
| 409 | expect: `h1`, |
| 410 | }, |
| 411 | { |
| 412 | // tag in quotes |
| 413 | input: `<h1">">h1</h1>`, |
| 414 | expect: `h1`, |
| 415 | }, |
| 416 | { |
| 417 | // multiple tags |
| 418 | input: `<h1><b>h1</b></h1>`, |
| 419 | expect: `h1`, |
| 420 | }, |
| 421 | { |
| 422 | // unclosed tag — trailing text must be stripped, not emitted |
| 423 | input: `<h1`, |
| 424 | expect: ``, |
| 425 | }, |
| 426 | { |
| 427 | // false start — second '<' increments depth, single '>' only closes one level |
| 428 | input: `<h1<b>hi`, |
| 429 | expect: ``, |
| 430 | }, |
| 431 | { |
| 432 | // XSS bypass via double opening bracket |
| 433 | input: `<<>img src=x onerror=alert('XSS')>`, |
| 434 | expect: ``, |
| 435 | }, |
| 436 | { |
| 437 | // stacked angle brackets (PHP strip_tags parity) |
| 438 | input: `<<<<<>>>>><b>hello</b>`, |
| 439 | expect: `hello`, |
| 440 | }, |
| 441 | { |
| 442 | // unclosed tag strips trailing text |
| 443 | input: `hello <world`, |
| 444 | expect: `hello `, |
| 445 | }, |
| 446 | { |
| 447 | // '>' inside double-quoted attribute must not close tag early |
| 448 | input: `<a href="foo>bar">text</a>`, |
| 449 | expect: `text`, |
| 450 | }, |
| 451 | { |
| 452 | // '>' inside single-quoted attribute must not close tag early |
| 453 | input: `<a href='foo>bar'>text</a>`, |
| 454 | expect: `text`, |
| 455 | }, |
| 456 | { |
| 457 | // stray '>' with no opening '<' is preserved |
| 458 | input: `stray > bracket`, |
| 459 | expect: `stray > bracket`, |
| 460 | }, |
| 461 | } { |
| 462 | actual := tplContext.funcStripHTMLscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#funcStripHTML().(test.input) |
| 463 | if actual != test.expect { |
| 464 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected %s, found %s. Input was StripHTML(%s)", i, test.expect, actual, test.input) |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | func TestFileListingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TestFileListing().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 470 | for i, test := range []struct { |
| 471 | fileNames []string |
| 472 | inputBase string |
| 473 | shouldErr bool |
| 474 | verifyErr func(error) bool |
| 475 | }{ |
| 476 | { |
| 477 | // directory and files exist |
| 478 | fileNames: []string{"file1", "file2"}, |
| 479 | shouldErr: false, |
| 480 | }, |
| 481 | { |
| 482 | // directory exists, no files |
| 483 | fileNames: []string{}, |
| 484 | shouldErr: false, |
| 485 | }, |
| 486 | { |
| 487 | // file or directory does not exist |
| 488 | fileNames: nil, |
| 489 | inputBase: "doesNotExist", |
| 490 | shouldErr: true, |
| 491 | verifyErr: func(err error) bool { return errorsscip-go gomod github.com/golang/go/src go1.25.1 errors/.Isscip-go gomod github.com/golang/go/src go1.25.1 errors/Is().(err, fsscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/.ErrNotExistscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/ErrNotExist.) }, |
| 492 | }, |
| 493 | { |
| 494 | // directory and files exist, but path to a file |
| 495 | fileNames: []string{"file1", "file2"}, |
| 496 | inputBase: "file1", |
| 497 | shouldErr: true, |
| 498 | verifyErr: func(err error) bool { |
| 499 | return stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.1 strings/HasSuffix().(err.Error(), "is not a directory") |
| 500 | }, |
| 501 | }, |
| 502 | { |
| 503 | // try to escape Context Root |
| 504 | fileNames: nil, |
| 505 | inputBase: filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/Join().("..", "..", "..", "..", "..", "etc"), |
| 506 | shouldErr: true, |
| 507 | verifyErr: func(err error) bool { return errorsscip-go gomod github.com/golang/go/src go1.25.1 errors/.Isscip-go gomod github.com/golang/go/src go1.25.1 errors/Is().(err, fsscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/.ErrNotExistscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/ErrNotExist.) }, |
| 508 | }, |
| 509 | } { |
| 510 | tplContext := getContextOrFailscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/getContextOrFail().(t) |
| 511 | var dirPath string |
| 512 | var err error |
| 513 | |
| 514 | // create files for test case |
| 515 | if test.fileNames != nil { |
| 516 | dirPath, err = osscip-go gomod github.com/golang/go/src go1.25.1 os/.MkdirTempscip-go gomod github.com/golang/go/src go1.25.1 os/MkdirTemp().(fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Sprintf().("%s", tplContext.Rootscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#Root.), "caddy_ctxtest") |
| 517 | if err != nil { |
| 518 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Test %d: Expected no error creating directory, got: '%s'", i, err.Error()) |
| 519 | } |
| 520 | for _, name := range test.fileNames { |
| 521 | absFilePath := filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/Join().(dirPath, name) |
| 522 | if err = osscip-go gomod github.com/golang/go/src go1.25.1 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.1 os/WriteFile().(absFilePath, []byte(""), osscip-go gomod github.com/golang/go/src go1.25.1 os/.ModePermscip-go gomod github.com/golang/go/src go1.25.1 os/ModePerm.); err != nil { |
| 523 | osscip-go gomod github.com/golang/go/src go1.25.1 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.25.1 os/RemoveAll().(dirPath) |
| 524 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Test %d: Expected no error creating file, got: '%s'", i, err.Error()) |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | // perform test |
| 530 | input := filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.ToSlashscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/ToSlash().(filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/Join().(filepathscip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/.Basescip-go gomod github.com/golang/go/src go1.25.1 `path/filepath`/Base().(dirPath), test.inputBase)) |
| 531 | actual, err := tplContext.funcListFilesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#funcListFiles().(input) |
| 532 | if err != nil { |
| 533 | if !test.shouldErr { |
| 534 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected no error, got: '%s'", i, err) |
| 535 | } else if !test.verifyErr(err) { |
| 536 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Could not verify error content, got: '%s'", i, err) |
| 537 | } |
| 538 | } else if test.shouldErr { |
| 539 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected error but had none", i) |
| 540 | } else { |
| 541 | numFiles := len(test.fileNames) |
| 542 | // reflect.DeepEqual does not consider two empty slices to be equal |
| 543 | if numFiles == 0 && len(actual) != 0 { |
| 544 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected files %v, got: %v", |
| 545 | i, test.fileNames, actual) |
| 546 | } else { |
| 547 | sortscip-go gomod github.com/golang/go/src go1.25.1 sort/.Stringsscip-go gomod github.com/golang/go/src go1.25.1 sort/Strings().(actual) |
| 548 | if numFiles > 0 && !reflectscip-go gomod github.com/golang/go/src go1.25.1 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.1 reflect/DeepEqual().(test.fileNames, actual) { |
| 549 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected files %v, got: %v", |
| 550 | i, test.fileNames, actual) |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | if dirPath != "" { |
| 556 | if err := osscip-go gomod github.com/golang/go/src go1.25.1 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.25.1 os/RemoveAll().(dirPath); err != nil && !errorsscip-go gomod github.com/golang/go/src go1.25.1 errors/.Isscip-go gomod github.com/golang/go/src go1.25.1 errors/Is().(err, fsscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/.ErrNotExistscip-go gomod github.com/golang/go/src go1.25.1 `io/fs`/ErrNotExist.) { |
| 557 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Test %d: Expected no error removing temporary test directory, got: %v", i, err) |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | func TestSplitFrontMatterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TestSplitFrontMatter().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 564 | tplContext := getContextOrFailscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/getContextOrFail().(t) |
| 565 | |
| 566 | for i, test := range []struct { |
| 567 | input string |
| 568 | expect string |
| 569 | body string |
| 570 | }{ |
| 571 | { |
| 572 | // yaml with windows newline |
| 573 | input: "---\r\ntitle: Welcome\r\n---\r\n# Test\\r\\n", |
| 574 | expect: `Welcome`, |
| 575 | body: "\r\n# Test\\r\\n", |
| 576 | }, |
| 577 | { |
| 578 | // yaml |
| 579 | input: `--- |
| 580 | title: Welcome |
| 581 | --- |
| 582 | ### Test`, |
| 583 | expect: `Welcome`, |
| 584 | body: "\n### Test", |
| 585 | }, |
| 586 | { |
| 587 | // yaml with dots for closer |
| 588 | input: `--- |
| 589 | title: Welcome |
| 590 | ... |
| 591 | ### Test`, |
| 592 | expect: `Welcome`, |
| 593 | body: "\n### Test", |
| 594 | }, |
| 595 | { |
| 596 | // yaml with non-fence '...' line after closing fence (i.e. first matching closing fence should be used) |
| 597 | input: `--- |
| 598 | title: Welcome |
| 599 | --- |
| 600 | ### Test |
| 601 | ... |
| 602 | yeah`, |
| 603 | expect: `Welcome`, |
| 604 | body: "\n### Test\n...\nyeah", |
| 605 | }, |
| 606 | { |
| 607 | // toml |
| 608 | input: `+++ |
| 609 | title = "Welcome" |
| 610 | +++ |
| 611 | ### Test`, |
| 612 | expect: `Welcome`, |
| 613 | body: "\n### Test", |
| 614 | }, |
| 615 | { |
| 616 | // json |
| 617 | input: `{ |
| 618 | "title": "Welcome" |
| 619 | } |
| 620 | ### Test`, |
| 621 | expect: `Welcome`, |
| 622 | body: "\n### Test", |
| 623 | }, |
| 624 | } { |
| 625 | result, _ := tplContext.funcSplitFrontMatterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#funcSplitFrontMatter().(test.input) |
| 626 | if result.Metascip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/parsedMarkdownDoc#Meta.["title"] != test.expect { |
| 627 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected %s, found %s. Input was SplitFrontMatter(%s)", i, test.expect, result.Metascip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/parsedMarkdownDoc#Meta.["title"], test.input) |
| 628 | } |
| 629 | if result.Bodyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/parsedMarkdownDoc#Body. != test.body { |
| 630 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected body %s, found %s. Input was SplitFrontMatter(%s)", i, test.body, result.Bodyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/parsedMarkdownDoc#Body., test.input) |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | func TestHumanizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TestHumanize().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 636 | tplContext := getContextOrFailscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/getContextOrFail().(t) |
| 637 | for i, test := range []struct { |
| 638 | format string |
| 639 | inputData string |
| 640 | expect string |
| 641 | errorCase bool |
| 642 | verifyErr func(actual_string, substring string) bool |
| 643 | }{ |
| 644 | { |
| 645 | format: "size", |
| 646 | inputData: "2048000", |
| 647 | expect: "2.0 MB", |
| 648 | errorCase: false, |
| 649 | verifyErr: stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.1 strings/Contains()., |
| 650 | }, |
| 651 | { |
| 652 | format: "time", |
| 653 | inputData: "Fri, 05 May 2022 15:04:05 +0200", |
| 654 | expect: "ago", |
| 655 | errorCase: false, |
| 656 | verifyErr: stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.1 strings/HasSuffix()., |
| 657 | }, |
| 658 | { |
| 659 | format: "time:2006-Jan-02", |
| 660 | inputData: "2022-May-05", |
| 661 | expect: "ago", |
| 662 | errorCase: false, |
| 663 | verifyErr: stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.1 strings/HasSuffix()., |
| 664 | }, |
| 665 | { |
| 666 | format: "time", |
| 667 | inputData: "Fri, 05 May 2022 15:04:05 GMT+0200", |
| 668 | expect: "error:", |
| 669 | errorCase: true, |
| 670 | verifyErr: stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.1 strings/HasPrefix()., |
| 671 | }, |
| 672 | } { |
| 673 | if actual, err := tplContext.funcHumanizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#funcHumanize().(test.format, test.inputData); !test.verifyErr(actual, test.expect) { |
| 674 | if !test.errorCase { |
| 675 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected '%s' but got '%s'", i, test.expect, actual) |
| 676 | if err != nil { |
| 677 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: error: %s", i, err.Error()) |
| 678 | } |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | func getContextOrFailscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/getContextOrFail().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) TemplateContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext# { |
| 685 | tplContext, err := initTestContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/initTestContext().() |
| 686 | t.Cleanupscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Cleanup().(func() { |
| 687 | osscip-go gomod github.com/golang/go/src go1.25.1 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.25.1 os/RemoveAll().(string(tplContext.Rootscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#Root..(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Dirscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Dir#))) |
| 688 | }) |
| 689 | if err != nil { |
| 690 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("failed to prepare test context: %v", err) |
| 691 | } |
| 692 | return tplContext |
| 693 | } |
| 694 | |
| 695 | func initTestContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/initTestContext().() (TemplateContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#, error) { |
| 696 | body := bytesscip-go gomod github.com/golang/go/src go1.25.1 bytes/.NewBufferStringscip-go gomod github.com/golang/go/src go1.25.1 bytes/NewBufferString().("request body") |
| 697 | request, err := httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.NewRequestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/NewRequest().("GET", "https://example.com/foo/bar", body) |
| 698 | if err != nil { |
| 699 | return TemplateContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#{}, err |
| 700 | } |
| 701 | tmpDir, err := osscip-go gomod github.com/golang/go/src go1.25.1 os/.MkdirTempscip-go gomod github.com/golang/go/src go1.25.1 os/MkdirTemp().(osscip-go gomod github.com/golang/go/src go1.25.1 os/.TempDirscip-go gomod github.com/golang/go/src go1.25.1 os/TempDir().(), "caddy") |
| 702 | if err != nil { |
| 703 | return TemplateContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#{}, err |
| 704 | } |
| 705 | return TemplateContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#{ |
| 706 | Rootscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#Root.: httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Dirscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Dir#(tmpDir), |
| 707 | Reqscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#Req.: request, |
| 708 | RespHeaderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/TemplateContext#RespHeader.: WrappedHeaderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/templates`/WrappedHeader#{make(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#)}, |
| 709 | }, nil |
| 710 | } |