2019-07-01 06:07:58 +08:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-06-19 01:13:12 +08:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-10-16 23:09:16 +08:00
|
|
|
"context"
|
2024-01-02 13:48:55 +08:00
|
|
|
"errors"
|
2019-06-19 01:13:12 +08:00
|
|
|
"fmt"
|
2024-01-02 13:48:55 +08:00
|
|
|
"io/fs"
|
2019-06-19 01:13:12 +08:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2021-10-16 23:09:16 +08:00
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
2019-06-19 01:13:12 +08:00
|
|
|
)
|
|
|
|
|
2024-01-02 13:48:55 +08:00
|
|
|
type handle struct{}
|
2021-10-16 23:09:16 +08:00
|
|
|
|
|
|
|
func (h *handle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Header.Get("Accept-Encoding") == "identity" {
|
|
|
|
w.Write([]byte("good contents"))
|
|
|
|
} else {
|
|
|
|
w.Write([]byte("bad cause Accept-Encoding: " + r.Header.Get("Accept-Encoding")))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTPInclude(t *testing.T) {
|
|
|
|
tplContext := getContextOrFail(t)
|
|
|
|
for i, test := range []struct {
|
|
|
|
uri string
|
|
|
|
handler *handle
|
|
|
|
expect string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
uri: "https://example.com/foo/bar",
|
|
|
|
handler: &handle{},
|
|
|
|
expect: "good contents",
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
ctx := context.WithValue(tplContext.Req.Context(), caddyhttp.ServerCtxKey, test.handler)
|
|
|
|
tplContext.Req = tplContext.Req.WithContext(ctx)
|
|
|
|
tplContext.Req.Header.Add("Accept-Encoding", "gzip")
|
|
|
|
result, err := tplContext.funcHTTPInclude(test.uri)
|
|
|
|
if result != test.expect {
|
|
|
|
t.Errorf("Test %d: expected '%s' but got '%s'", i, test.expect, result)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test %d: got error: %v", i, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 01:13:12 +08:00
|
|
|
func TestMarkdown(t *testing.T) {
|
2021-10-16 23:09:16 +08:00
|
|
|
tplContext := getContextOrFail(t)
|
2019-06-19 01:13:12 +08:00
|
|
|
|
|
|
|
for i, test := range []struct {
|
|
|
|
body string
|
2020-04-07 02:50:54 +08:00
|
|
|
expect string
|
2019-06-19 01:13:12 +08:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
body: "- str1\n- str2\n",
|
|
|
|
expect: "<ul>\n<li>str1</li>\n<li>str2</li>\n</ul>\n",
|
|
|
|
},
|
|
|
|
} {
|
2021-10-16 23:09:16 +08:00
|
|
|
result, err := tplContext.funcMarkdown(test.body)
|
2019-06-19 01:13:12 +08:00
|
|
|
if result != test.expect {
|
|
|
|
t.Errorf("Test %d: expected '%s' but got '%s'", i, test.expect, result)
|
|
|
|
}
|
2019-12-24 03:56:41 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test %d: got error: %v", i, result)
|
|
|
|
}
|
2019-06-19 01:13:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCookie(t *testing.T) {
|
|
|
|
for i, test := range []struct {
|
|
|
|
cookie *http.Cookie
|
|
|
|
cookieName string
|
|
|
|
expect string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// happy path
|
|
|
|
cookie: &http.Cookie{Name: "cookieName", Value: "cookieValue"},
|
|
|
|
cookieName: "cookieName",
|
|
|
|
expect: "cookieValue",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// try to get a non-existing cookie
|
|
|
|
cookie: &http.Cookie{Name: "cookieName", Value: "cookieValue"},
|
|
|
|
cookieName: "notExisting",
|
|
|
|
expect: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// partial name match
|
|
|
|
cookie: &http.Cookie{Name: "cookie", Value: "cookieValue"},
|
|
|
|
cookieName: "cook",
|
|
|
|
expect: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// cookie with optional fields
|
2020-08-01 06:54:18 +08:00
|
|
|
cookie: &http.Cookie{Name: "cookie", Value: "cookieValue", Path: "/path", Domain: "https://localhost", Expires: time.Now().Add(10 * time.Minute), MaxAge: 120},
|
2019-06-19 01:13:12 +08:00
|
|
|
cookieName: "cookie",
|
|
|
|
expect: "cookieValue",
|
|
|
|
},
|
|
|
|
} {
|
2021-10-16 23:09:16 +08:00
|
|
|
tplContext := getContextOrFail(t)
|
|
|
|
tplContext.Req.AddCookie(test.cookie)
|
|
|
|
actual := tplContext.Cookie(test.cookieName)
|
2019-06-19 01:13:12 +08:00
|
|
|
if actual != test.expect {
|
|
|
|
t.Errorf("Test %d: Expected cookie value '%s' but got '%s' for cookie with name '%s'",
|
|
|
|
i, test.expect, actual, test.cookieName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 02:29:37 +08:00
|
|
|
func TestImport(t *testing.T) {
|
|
|
|
for i, test := range []struct {
|
|
|
|
fileContent string
|
|
|
|
fileName string
|
|
|
|
shouldErr bool
|
|
|
|
expect string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// file exists, template is defined
|
|
|
|
fileContent: `{{ define "imported" }}text{{end}}`,
|
|
|
|
fileName: "file1",
|
|
|
|
shouldErr: false,
|
|
|
|
expect: `"imported"`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// file does not exit
|
|
|
|
fileContent: "",
|
|
|
|
fileName: "",
|
|
|
|
shouldErr: true,
|
|
|
|
},
|
|
|
|
} {
|
2021-10-16 23:09:16 +08:00
|
|
|
tplContext := getContextOrFail(t)
|
2021-09-21 02:29:37 +08:00
|
|
|
var absFilePath string
|
|
|
|
|
|
|
|
// create files for test case
|
|
|
|
if test.fileName != "" {
|
2021-10-16 23:09:16 +08:00
|
|
|
absFilePath := filepath.Join(fmt.Sprintf("%s", tplContext.Root), test.fileName)
|
2021-09-30 01:17:48 +08:00
|
|
|
if err := os.WriteFile(absFilePath, []byte(test.fileContent), os.ModePerm); err != nil {
|
2021-09-21 02:29:37 +08:00
|
|
|
os.Remove(absFilePath)
|
|
|
|
t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// perform test
|
2021-10-16 23:09:16 +08:00
|
|
|
tplContext.NewTemplate("parent")
|
|
|
|
actual, err := tplContext.funcImport(test.fileName)
|
|
|
|
templateWasDefined := strings.Contains(tplContext.tpl.DefinedTemplates(), test.expect)
|
2021-09-21 02:29:37 +08:00
|
|
|
if err != nil {
|
|
|
|
if !test.shouldErr {
|
|
|
|
t.Errorf("Test %d: Expected no error, got: '%s'", i, err)
|
|
|
|
}
|
|
|
|
} else if test.shouldErr {
|
|
|
|
t.Errorf("Test %d: Expected error but had none", i)
|
|
|
|
} else if !templateWasDefined && actual != "" {
|
|
|
|
// template should be defined, return value should be an empty string
|
2021-10-16 23:09:16 +08:00
|
|
|
t.Errorf("Test %d: Expected template %s to be define but got %s", i, test.expect, tplContext.tpl.DefinedTemplates())
|
2021-09-21 02:29:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if absFilePath != "" {
|
2024-01-02 13:48:55 +08:00
|
|
|
if err := os.Remove(absFilePath); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
2021-09-21 02:29:37 +08:00
|
|
|
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-30 01:29:40 +08:00
|
|
|
func TestNestedInclude(t *testing.T) {
|
|
|
|
for i, test := range []struct {
|
|
|
|
child string
|
|
|
|
childFile string
|
|
|
|
parent string
|
|
|
|
parentFile string
|
|
|
|
shouldErr bool
|
|
|
|
expect string
|
|
|
|
child2 string
|
|
|
|
child2File string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// include in parent
|
|
|
|
child: `{{ include "file1" }}`,
|
|
|
|
childFile: "file0",
|
|
|
|
parent: `{{ $content := "file2" }}{{ $p := include $content}}`,
|
|
|
|
parentFile: "file1",
|
|
|
|
shouldErr: false,
|
|
|
|
expect: ``,
|
|
|
|
child2: `This shouldn't show`,
|
|
|
|
child2File: "file2",
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
context := getContextOrFail(t)
|
|
|
|
var absFilePath string
|
|
|
|
var absFilePath0 string
|
|
|
|
var absFilePath1 string
|
|
|
|
var buf *bytes.Buffer
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// create files and for test case
|
|
|
|
if test.parentFile != "" {
|
|
|
|
absFilePath = filepath.Join(fmt.Sprintf("%s", context.Root), test.parentFile)
|
2023-06-17 11:27:57 +08:00
|
|
|
if err := os.WriteFile(absFilePath, []byte(test.parent), os.ModePerm); err != nil {
|
2021-11-30 01:29:40 +08:00
|
|
|
os.Remove(absFilePath)
|
|
|
|
t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if test.childFile != "" {
|
|
|
|
absFilePath0 = filepath.Join(fmt.Sprintf("%s", context.Root), test.childFile)
|
2023-06-17 11:27:57 +08:00
|
|
|
if err := os.WriteFile(absFilePath0, []byte(test.child), os.ModePerm); err != nil {
|
2021-11-30 01:29:40 +08:00
|
|
|
os.Remove(absFilePath0)
|
|
|
|
t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if test.child2File != "" {
|
|
|
|
absFilePath1 = filepath.Join(fmt.Sprintf("%s", context.Root), test.child2File)
|
2023-06-17 11:27:57 +08:00
|
|
|
if err := os.WriteFile(absFilePath1, []byte(test.child2), os.ModePerm); err != nil {
|
2021-11-30 01:29:40 +08:00
|
|
|
os.Remove(absFilePath0)
|
|
|
|
t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = bufPool.Get().(*bytes.Buffer)
|
|
|
|
buf.Reset()
|
|
|
|
defer bufPool.Put(buf)
|
|
|
|
buf.WriteString(test.child)
|
|
|
|
err = context.executeTemplateInBuffer(test.childFile, buf)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if !test.shouldErr {
|
|
|
|
t.Errorf("Test %d: Expected no error, got: '%s'", i, err)
|
|
|
|
}
|
|
|
|
} else if test.shouldErr {
|
|
|
|
t.Errorf("Test %d: Expected error but had none", i)
|
|
|
|
} else if buf.String() != test.expect {
|
|
|
|
//
|
|
|
|
t.Errorf("Test %d: Expected '%s' but got '%s'", i, test.expect, buf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if absFilePath != "" {
|
2024-01-02 13:48:55 +08:00
|
|
|
if err := os.Remove(absFilePath); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
2021-11-30 01:29:40 +08:00
|
|
|
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if absFilePath0 != "" {
|
2024-01-02 13:48:55 +08:00
|
|
|
if err := os.Remove(absFilePath0); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
2021-11-30 01:29:40 +08:00
|
|
|
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if absFilePath1 != "" {
|
2024-01-02 13:48:55 +08:00
|
|
|
if err := os.Remove(absFilePath1); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
2021-11-30 01:29:40 +08:00
|
|
|
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 02:29:37 +08:00
|
|
|
func TestInclude(t *testing.T) {
|
|
|
|
for i, test := range []struct {
|
|
|
|
fileContent string
|
|
|
|
fileName string
|
|
|
|
shouldErr bool
|
|
|
|
expect string
|
|
|
|
args string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// file exists, content is text only
|
|
|
|
fileContent: "text",
|
|
|
|
fileName: "file1",
|
|
|
|
shouldErr: false,
|
|
|
|
expect: "text",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// file exists, content is template
|
|
|
|
fileContent: "{{ if . }}text{{ end }}",
|
|
|
|
fileName: "file1",
|
|
|
|
shouldErr: false,
|
|
|
|
expect: "text",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// file does not exit
|
|
|
|
fileContent: "",
|
|
|
|
fileName: "",
|
|
|
|
shouldErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// args
|
|
|
|
fileContent: "{{ index .Args 0 }}",
|
|
|
|
fileName: "file1",
|
|
|
|
shouldErr: false,
|
|
|
|
args: "text",
|
|
|
|
expect: "text",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// args, reference arg out of range
|
|
|
|
fileContent: "{{ index .Args 1 }}",
|
|
|
|
fileName: "file1",
|
|
|
|
shouldErr: true,
|
|
|
|
args: "text",
|
|
|
|
},
|
|
|
|
} {
|
2021-10-16 23:09:16 +08:00
|
|
|
tplContext := getContextOrFail(t)
|
2021-09-21 02:29:37 +08:00
|
|
|
var absFilePath string
|
|
|
|
|
|
|
|
// create files for test case
|
|
|
|
if test.fileName != "" {
|
2021-10-16 23:09:16 +08:00
|
|
|
absFilePath := filepath.Join(fmt.Sprintf("%s", tplContext.Root), test.fileName)
|
2021-09-30 01:17:48 +08:00
|
|
|
if err := os.WriteFile(absFilePath, []byte(test.fileContent), os.ModePerm); err != nil {
|
2021-09-21 02:29:37 +08:00
|
|
|
os.Remove(absFilePath)
|
|
|
|
t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// perform test
|
2021-10-16 23:09:16 +08:00
|
|
|
actual, err := tplContext.funcInclude(test.fileName, test.args)
|
2021-09-21 02:29:37 +08:00
|
|
|
if err != nil {
|
|
|
|
if !test.shouldErr {
|
|
|
|
t.Errorf("Test %d: Expected no error, got: '%s'", i, err)
|
|
|
|
}
|
|
|
|
} else if test.shouldErr {
|
|
|
|
t.Errorf("Test %d: Expected error but had none", i)
|
|
|
|
} else if actual != test.expect {
|
|
|
|
t.Errorf("Test %d: Expected %s but got %s", i, test.expect, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
if absFilePath != "" {
|
2024-01-02 13:48:55 +08:00
|
|
|
if err := os.Remove(absFilePath); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
2021-09-21 02:29:37 +08:00
|
|
|
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 01:13:12 +08:00
|
|
|
func TestCookieMultipleCookies(t *testing.T) {
|
2021-10-16 23:09:16 +08:00
|
|
|
tplContext := getContextOrFail(t)
|
2019-06-19 01:13:12 +08:00
|
|
|
|
|
|
|
cookieNameBase, cookieValueBase := "cookieName", "cookieValue"
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
2021-10-16 23:09:16 +08:00
|
|
|
tplContext.Req.AddCookie(&http.Cookie{
|
2019-06-19 01:13:12 +08:00
|
|
|
Name: fmt.Sprintf("%s%d", cookieNameBase, i),
|
|
|
|
Value: fmt.Sprintf("%s%d", cookieValueBase, i),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
expectedCookieVal := fmt.Sprintf("%s%d", cookieValueBase, i)
|
2021-10-16 23:09:16 +08:00
|
|
|
actualCookieVal := tplContext.Cookie(fmt.Sprintf("%s%d", cookieNameBase, i))
|
2019-06-19 01:13:12 +08:00
|
|
|
if actualCookieVal != expectedCookieVal {
|
|
|
|
t.Errorf("Expected cookie value %s, found %s", expectedCookieVal, actualCookieVal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIP(t *testing.T) {
|
2021-10-16 23:09:16 +08:00
|
|
|
tplContext := getContextOrFail(t)
|
2019-06-19 01:13:12 +08:00
|
|
|
for i, test := range []struct {
|
|
|
|
inputRemoteAddr string
|
|
|
|
expect string
|
|
|
|
}{
|
|
|
|
{"1.1.1.1:1111", "1.1.1.1"},
|
|
|
|
{"1.1.1.1", "1.1.1.1"},
|
|
|
|
{"[::1]:11", "::1"},
|
|
|
|
{"[2001:db8:a0b:12f0::1]", "[2001:db8:a0b:12f0::1]"},
|
|
|
|
{`[fe80:1::3%eth0]:44`, `fe80:1::3%eth0`},
|
|
|
|
} {
|
2021-10-16 23:09:16 +08:00
|
|
|
tplContext.Req.RemoteAddr = test.inputRemoteAddr
|
|
|
|
if actual := tplContext.RemoteIP(); actual != test.expect {
|
2019-06-19 01:13:12 +08:00
|
|
|
t.Errorf("Test %d: Expected %s but got %s", i, test.expect, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStripHTML(t *testing.T) {
|
2021-10-16 23:09:16 +08:00
|
|
|
tplContext := getContextOrFail(t)
|
2019-06-19 01:13:12 +08:00
|
|
|
|
|
|
|
for i, test := range []struct {
|
|
|
|
input string
|
|
|
|
expect string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// no tags
|
|
|
|
input: `h1`,
|
|
|
|
expect: `h1`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// happy path
|
|
|
|
input: `<h1>h1</h1>`,
|
|
|
|
expect: `h1`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// tag in quotes
|
|
|
|
input: `<h1">">h1</h1>`,
|
|
|
|
expect: `h1`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// multiple tags
|
|
|
|
input: `<h1><b>h1</b></h1>`,
|
|
|
|
expect: `h1`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// tags not closed
|
|
|
|
input: `<h1`,
|
|
|
|
expect: `<h1`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// false start
|
|
|
|
input: `<h1<b>hi`,
|
|
|
|
expect: `<h1hi`,
|
|
|
|
},
|
|
|
|
} {
|
2021-10-16 23:09:16 +08:00
|
|
|
actual := tplContext.funcStripHTML(test.input)
|
2019-06-19 01:13:12 +08:00
|
|
|
if actual != test.expect {
|
|
|
|
t.Errorf("Test %d: Expected %s, found %s. Input was StripHTML(%s)", i, test.expect, actual, test.input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileListing(t *testing.T) {
|
|
|
|
for i, test := range []struct {
|
|
|
|
fileNames []string
|
|
|
|
inputBase string
|
|
|
|
shouldErr bool
|
|
|
|
verifyErr func(error) bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// directory and files exist
|
|
|
|
fileNames: []string{"file1", "file2"},
|
|
|
|
shouldErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// directory exists, no files
|
|
|
|
fileNames: []string{},
|
|
|
|
shouldErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// file or directory does not exist
|
|
|
|
fileNames: nil,
|
|
|
|
inputBase: "doesNotExist",
|
|
|
|
shouldErr: true,
|
2024-01-02 13:48:55 +08:00
|
|
|
verifyErr: func(err error) bool { return errors.Is(err, fs.ErrNotExist) },
|
2019-06-19 01:13:12 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// directory and files exist, but path to a file
|
|
|
|
fileNames: []string{"file1", "file2"},
|
|
|
|
inputBase: "file1",
|
|
|
|
shouldErr: true,
|
|
|
|
verifyErr: func(err error) bool {
|
|
|
|
return strings.HasSuffix(err.Error(), "is not a directory")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// try to escape Context Root
|
|
|
|
fileNames: nil,
|
|
|
|
inputBase: filepath.Join("..", "..", "..", "..", "..", "etc"),
|
|
|
|
shouldErr: true,
|
2024-01-02 13:48:55 +08:00
|
|
|
verifyErr: func(err error) bool { return errors.Is(err, fs.ErrNotExist) },
|
2019-06-19 01:13:12 +08:00
|
|
|
},
|
|
|
|
} {
|
2021-10-16 23:09:16 +08:00
|
|
|
tplContext := getContextOrFail(t)
|
2019-06-19 01:13:12 +08:00
|
|
|
var dirPath string
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// create files for test case
|
|
|
|
if test.fileNames != nil {
|
2021-10-16 23:09:16 +08:00
|
|
|
dirPath, err = os.MkdirTemp(fmt.Sprintf("%s", tplContext.Root), "caddy_ctxtest")
|
2019-06-19 01:13:12 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: Expected no error creating directory, got: '%s'", i, err.Error())
|
|
|
|
}
|
|
|
|
for _, name := range test.fileNames {
|
|
|
|
absFilePath := filepath.Join(dirPath, name)
|
2021-09-30 01:17:48 +08:00
|
|
|
if err = os.WriteFile(absFilePath, []byte(""), os.ModePerm); err != nil {
|
2019-06-19 01:13:12 +08:00
|
|
|
os.RemoveAll(dirPath)
|
|
|
|
t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// perform test
|
|
|
|
input := filepath.ToSlash(filepath.Join(filepath.Base(dirPath), test.inputBase))
|
2021-10-16 23:09:16 +08:00
|
|
|
actual, err := tplContext.funcListFiles(input)
|
2019-06-19 01:13:12 +08:00
|
|
|
if err != nil {
|
|
|
|
if !test.shouldErr {
|
|
|
|
t.Errorf("Test %d: Expected no error, got: '%s'", i, err)
|
|
|
|
} else if !test.verifyErr(err) {
|
|
|
|
t.Errorf("Test %d: Could not verify error content, got: '%s'", i, err)
|
|
|
|
}
|
|
|
|
} else if test.shouldErr {
|
|
|
|
t.Errorf("Test %d: Expected error but had none", i)
|
|
|
|
} else {
|
|
|
|
numFiles := len(test.fileNames)
|
|
|
|
// reflect.DeepEqual does not consider two empty slices to be equal
|
|
|
|
if numFiles == 0 && len(actual) != 0 {
|
|
|
|
t.Errorf("Test %d: Expected files %v, got: %v",
|
|
|
|
i, test.fileNames, actual)
|
|
|
|
} else {
|
|
|
|
sort.Strings(actual)
|
|
|
|
if numFiles > 0 && !reflect.DeepEqual(test.fileNames, actual) {
|
|
|
|
t.Errorf("Test %d: Expected files %v, got: %v",
|
|
|
|
i, test.fileNames, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if dirPath != "" {
|
2024-01-02 13:48:55 +08:00
|
|
|
if err := os.RemoveAll(dirPath); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
2019-06-19 01:13:12 +08:00
|
|
|
t.Fatalf("Test %d: Expected no error removing temporary test directory, got: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 03:01:04 +08:00
|
|
|
func TestSplitFrontMatter(t *testing.T) {
|
2021-10-16 23:09:16 +08:00
|
|
|
tplContext := getContextOrFail(t)
|
2020-05-19 03:01:04 +08:00
|
|
|
|
|
|
|
for i, test := range []struct {
|
|
|
|
input string
|
|
|
|
expect string
|
2020-06-16 02:38:51 +08:00
|
|
|
body string
|
2020-05-19 03:01:04 +08:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
// yaml with windows newline
|
|
|
|
input: "---\r\ntitle: Welcome\r\n---\r\n# Test\\r\\n",
|
|
|
|
expect: `Welcome`,
|
2020-06-16 02:38:51 +08:00
|
|
|
body: "\r\n# Test\\r\\n",
|
2020-05-19 03:01:04 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// yaml
|
|
|
|
input: `---
|
|
|
|
title: Welcome
|
|
|
|
---
|
|
|
|
### Test`,
|
|
|
|
expect: `Welcome`,
|
2020-06-16 02:38:51 +08:00
|
|
|
body: "\n### Test",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// yaml with dots for closer
|
|
|
|
input: `---
|
|
|
|
title: Welcome
|
|
|
|
...
|
|
|
|
### Test`,
|
|
|
|
expect: `Welcome`,
|
|
|
|
body: "\n### Test",
|
2020-05-19 03:01:04 +08:00
|
|
|
},
|
2020-07-09 06:46:56 +08:00
|
|
|
{
|
|
|
|
// yaml with non-fence '...' line after closing fence (i.e. first matching closing fence should be used)
|
|
|
|
input: `---
|
|
|
|
title: Welcome
|
|
|
|
---
|
|
|
|
### Test
|
|
|
|
...
|
|
|
|
yeah`,
|
|
|
|
expect: `Welcome`,
|
|
|
|
body: "\n### Test\n...\nyeah",
|
|
|
|
},
|
2020-05-19 03:01:04 +08:00
|
|
|
{
|
|
|
|
// toml
|
|
|
|
input: `+++
|
|
|
|
title = "Welcome"
|
|
|
|
+++
|
|
|
|
### Test`,
|
|
|
|
expect: `Welcome`,
|
2020-06-16 02:38:51 +08:00
|
|
|
body: "\n### Test",
|
2020-05-19 03:01:04 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// json
|
|
|
|
input: `{
|
|
|
|
"title": "Welcome"
|
|
|
|
}
|
|
|
|
### Test`,
|
|
|
|
expect: `Welcome`,
|
2020-06-16 02:38:51 +08:00
|
|
|
body: "\n### Test",
|
2020-05-19 03:01:04 +08:00
|
|
|
},
|
|
|
|
} {
|
2021-10-16 23:09:16 +08:00
|
|
|
result, _ := tplContext.funcSplitFrontMatter(test.input)
|
2020-05-19 03:01:04 +08:00
|
|
|
if result.Meta["title"] != test.expect {
|
|
|
|
t.Errorf("Test %d: Expected %s, found %s. Input was SplitFrontMatter(%s)", i, test.expect, result.Meta["title"], test.input)
|
|
|
|
}
|
2020-06-16 02:38:51 +08:00
|
|
|
if result.Body != test.body {
|
|
|
|
t.Errorf("Test %d: Expected body %s, found %s. Input was SplitFrontMatter(%s)", i, test.body, result.Body, test.input)
|
|
|
|
}
|
2020-05-19 03:01:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 07:47:08 +08:00
|
|
|
func TestHumanize(t *testing.T) {
|
|
|
|
tplContext := getContextOrFail(t)
|
|
|
|
for i, test := range []struct {
|
|
|
|
format string
|
|
|
|
inputData string
|
|
|
|
expect string
|
|
|
|
errorCase bool
|
|
|
|
verifyErr func(actual_string, substring string) bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
format: "size",
|
|
|
|
inputData: "2048000",
|
|
|
|
expect: "2.0 MB",
|
|
|
|
errorCase: false,
|
|
|
|
verifyErr: strings.Contains,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
format: "time",
|
|
|
|
inputData: "Fri, 05 May 2022 15:04:05 +0200",
|
|
|
|
expect: "ago",
|
|
|
|
errorCase: false,
|
|
|
|
verifyErr: strings.HasSuffix,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
format: "time:2006-Jan-02",
|
|
|
|
inputData: "2022-May-05",
|
|
|
|
expect: "ago",
|
|
|
|
errorCase: false,
|
|
|
|
verifyErr: strings.HasSuffix,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
format: "time",
|
|
|
|
inputData: "Fri, 05 May 2022 15:04:05 GMT+0200",
|
|
|
|
expect: "error:",
|
|
|
|
errorCase: true,
|
|
|
|
verifyErr: strings.HasPrefix,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
if actual, err := tplContext.funcHumanize(test.format, test.inputData); !test.verifyErr(actual, test.expect) {
|
|
|
|
if !test.errorCase {
|
|
|
|
t.Errorf("Test %d: Expected '%s' but got '%s'", i, test.expect, actual)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test %d: error: %s", i, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-01 10:17:23 +08:00
|
|
|
func getContextOrFail(t *testing.T) TemplateContext {
|
2021-10-16 23:09:16 +08:00
|
|
|
tplContext, err := initTestContext()
|
2022-01-25 05:41:08 +08:00
|
|
|
t.Cleanup(func() {
|
|
|
|
os.RemoveAll(string(tplContext.Root.(http.Dir)))
|
|
|
|
})
|
2019-06-19 01:13:12 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to prepare test context: %v", err)
|
|
|
|
}
|
2021-10-16 23:09:16 +08:00
|
|
|
return tplContext
|
2019-06-19 01:13:12 +08:00
|
|
|
}
|
|
|
|
|
2021-05-01 10:17:23 +08:00
|
|
|
func initTestContext() (TemplateContext, error) {
|
2019-06-19 01:13:12 +08:00
|
|
|
body := bytes.NewBufferString("request body")
|
|
|
|
request, err := http.NewRequest("GET", "https://example.com/foo/bar", body)
|
|
|
|
if err != nil {
|
2021-05-01 10:17:23 +08:00
|
|
|
return TemplateContext{}, err
|
2019-06-19 01:13:12 +08:00
|
|
|
}
|
2022-01-25 05:41:08 +08:00
|
|
|
tmpDir, err := os.MkdirTemp(os.TempDir(), "caddy")
|
|
|
|
if err != nil {
|
|
|
|
return TemplateContext{}, err
|
|
|
|
}
|
2021-05-01 10:17:23 +08:00
|
|
|
return TemplateContext{
|
2022-01-25 05:41:08 +08:00
|
|
|
Root: http.Dir(tmpDir),
|
2019-06-19 01:13:12 +08:00
|
|
|
Req: request,
|
2021-05-01 10:17:23 +08:00
|
|
|
RespHeader: WrappedHeader{make(http.Header)},
|
2019-06-19 01:13:12 +08:00
|
|
|
}, nil
|
|
|
|
}
|