@marcel91200 / caddy Mirror · read-only / commits / 483e31b9
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 14, 05:15 UTC

templates: trim windows whitespace in SplitFrontMatter; fix #3386 (#3387)

* add test case for SplitFrontMatter showing issue with windows newline * fix issue with windows newline when using SplitFrontMatter * Update modules/caddyhttp/templates/frontmatter.go Co-authored-by: Francis Lavoie <lavofr@gmail.com> * make it mere explicit what is trimmed from firstLine * Update modules/caddyhttp/templates/frontmatter.go Co-authored-by: Francis Lavoie <lavofr@gmail.com> Co-authored-by: Matt Holt <mholt@users.noreply.github.com>

Thorkild Gregersen committed May 18, 2020 at 21:01 UTC 483e31b9788f57b0a1adc153574878d0c7c4feb0
2 files changed +48
modules/caddyhttp/templates/frontmatter.go
+3
@@ -30,6 +30,9 @@ func extractFrontMatter(input string) (map[string]interface{}, string, error) {
30 }
31 firstLine := input[firstLineStart:firstLineEnd]
32
33 + // ensure residue windows carriage return byte is removed
34 + firstLine = strings.TrimSpace(firstLine)
35 +
36 // see what kind of front matter there is, if any
37 var closingFence string
38 var fmParser func([]byte) (map[string]interface{}, error)
modules/caddyhttp/templates/tplcontext_test.go
+45
@@ -284,6 +284,51 @@ func TestFileListing(t *testing.T) {
284 }
285 }
286
287 +func TestSplitFrontMatter(t *testing.T) {
288 + context := getContextOrFail(t)
289 +
290 + for i, test := range []struct {
291 + input string
292 + expect string
293 + }{
294 + {
295 + // yaml with windows newline
296 + input: "---\r\ntitle: Welcome\r\n---\r\n# Test\\r\\n",
297 + expect: `Welcome`,
298 + },
299 + {
300 + // yaml
301 + input: `---
302 +title: Welcome
303 +---
304 +### Test`,
305 + expect: `Welcome`,
306 + },
307 + {
308 + // toml
309 + input: `+++
310 +title = "Welcome"
311 ++++
312 +### Test`,
313 + expect: `Welcome`,
314 + },
315 + {
316 + // json
317 + input: `{
318 + "title": "Welcome"
319 +}
320 +### Test`,
321 + expect: `Welcome`,
322 + },
323 + } {
324 + result, _ := context.funcSplitFrontMatter(test.input)
325 + if result.Meta["title"] != test.expect {
326 + t.Errorf("Test %d: Expected %s, found %s. Input was SplitFrontMatter(%s)", i, test.expect, result.Meta["title"], test.input)
327 + }
328 + }
329 +
330 +}
331 +
332 func getContextOrFail(t *testing.T) templateContext {
333 context, err := initTestContext()
334 if err != nil {