mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-23 22:27:38 -05:00
153 lines
4.5 KiB
Go
153 lines
4.5 KiB
Go
|
package markdown
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
"testing"
|
||
|
"text/template"
|
||
|
|
||
|
"github.com/mholt/caddy"
|
||
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||
|
)
|
||
|
|
||
|
func TestSetup(t *testing.T) {
|
||
|
err := setup(caddy.NewTestController(`markdown /blog`))
|
||
|
if err != nil {
|
||
|
t.Errorf("Expected no errors, got: %v", err)
|
||
|
}
|
||
|
mids := httpserver.GetConfig("").Middleware()
|
||
|
if len(mids) == 0 {
|
||
|
t.Fatal("Expected middleware, got 0 instead")
|
||
|
}
|
||
|
|
||
|
handler := mids[0](httpserver.EmptyNext)
|
||
|
myHandler, ok := handler.(Markdown)
|
||
|
|
||
|
if !ok {
|
||
|
t.Fatalf("Expected handler to be type Markdown, got: %#v", handler)
|
||
|
}
|
||
|
|
||
|
if myHandler.Configs[0].PathScope != "/blog" {
|
||
|
t.Errorf("Expected /blog as the Path Scope")
|
||
|
}
|
||
|
if len(myHandler.Configs[0].Extensions) != 3 {
|
||
|
t.Error("Expected 3 markdown extensions")
|
||
|
}
|
||
|
for _, key := range []string{".md", ".markdown", ".mdown"} {
|
||
|
if ext, ok := myHandler.Configs[0].Extensions[key]; !ok {
|
||
|
t.Errorf("Expected extensions to contain %v", ext)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestMarkdownParse(t *testing.T) {
|
||
|
tests := []struct {
|
||
|
inputMarkdownConfig string
|
||
|
shouldErr bool
|
||
|
expectedMarkdownConfig []Config
|
||
|
}{
|
||
|
|
||
|
{`markdown /blog {
|
||
|
ext .md .txt
|
||
|
css /resources/css/blog.css
|
||
|
js /resources/js/blog.js
|
||
|
}`, false, []Config{{
|
||
|
PathScope: "/blog",
|
||
|
Extensions: map[string]struct{}{
|
||
|
".md": {},
|
||
|
".txt": {},
|
||
|
},
|
||
|
Styles: []string{"/resources/css/blog.css"},
|
||
|
Scripts: []string{"/resources/js/blog.js"},
|
||
|
Template: GetDefaultTemplate(),
|
||
|
}}},
|
||
|
{`markdown /blog {
|
||
|
ext .md
|
||
|
template tpl_with_include.html
|
||
|
}`, false, []Config{{
|
||
|
PathScope: "/blog",
|
||
|
Extensions: map[string]struct{}{
|
||
|
".md": {},
|
||
|
},
|
||
|
Template: GetDefaultTemplate(),
|
||
|
}}},
|
||
|
}
|
||
|
// Setup the extra template
|
||
|
tmpl := tests[1].expectedMarkdownConfig[0].Template
|
||
|
SetTemplate(tmpl, "", "./testdata/tpl_with_include.html")
|
||
|
|
||
|
for i, test := range tests {
|
||
|
c := caddy.NewTestController(test.inputMarkdownConfig)
|
||
|
httpserver.GetConfig("").Root = "./testdata"
|
||
|
actualMarkdownConfigs, err := markdownParse(c)
|
||
|
|
||
|
if err == nil && test.shouldErr {
|
||
|
t.Errorf("Test %d didn't error, but it should have", i)
|
||
|
} else if err != nil && !test.shouldErr {
|
||
|
t.Errorf("Test %d errored, but it shouldn't have; got '%v'", i, err)
|
||
|
}
|
||
|
if len(actualMarkdownConfigs) != len(test.expectedMarkdownConfig) {
|
||
|
t.Fatalf("Test %d expected %d no of WebSocket configs, but got %d ",
|
||
|
i, len(test.expectedMarkdownConfig), len(actualMarkdownConfigs))
|
||
|
}
|
||
|
for j, actualMarkdownConfig := range actualMarkdownConfigs {
|
||
|
|
||
|
if actualMarkdownConfig.PathScope != test.expectedMarkdownConfig[j].PathScope {
|
||
|
t.Errorf("Test %d expected %dth Markdown PathScope to be %s , but got %s",
|
||
|
i, j, test.expectedMarkdownConfig[j].PathScope, actualMarkdownConfig.PathScope)
|
||
|
}
|
||
|
|
||
|
if fmt.Sprint(actualMarkdownConfig.Styles) != fmt.Sprint(test.expectedMarkdownConfig[j].Styles) {
|
||
|
t.Errorf("Test %d expected %dth Markdown Config Styles to be %s , but got %s",
|
||
|
i, j, fmt.Sprint(test.expectedMarkdownConfig[j].Styles), fmt.Sprint(actualMarkdownConfig.Styles))
|
||
|
}
|
||
|
if fmt.Sprint(actualMarkdownConfig.Scripts) != fmt.Sprint(test.expectedMarkdownConfig[j].Scripts) {
|
||
|
t.Errorf("Test %d expected %dth Markdown Config Scripts to be %s , but got %s",
|
||
|
i, j, fmt.Sprint(test.expectedMarkdownConfig[j].Scripts), fmt.Sprint(actualMarkdownConfig.Scripts))
|
||
|
}
|
||
|
if ok, tx, ty := equalTemplates(actualMarkdownConfig.Template, test.expectedMarkdownConfig[j].Template); !ok {
|
||
|
t.Errorf("Test %d the %dth Markdown Config Templates did not match, expected %s to be %s", i, j, tx, ty)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func equalTemplates(i, j *template.Template) (bool, string, string) {
|
||
|
// Just in case :)
|
||
|
if i == j {
|
||
|
return true, "", ""
|
||
|
}
|
||
|
|
||
|
// We can't do much here, templates can't really be compared. However,
|
||
|
// we can execute the templates and compare their outputs to be reasonably
|
||
|
// sure that they're the same.
|
||
|
|
||
|
// This is exceedingly ugly.
|
||
|
ctx := httpserver.Context{
|
||
|
Root: http.Dir("./testdata"),
|
||
|
}
|
||
|
|
||
|
md := Data{
|
||
|
Context: ctx,
|
||
|
Doc: make(map[string]string),
|
||
|
DocFlags: make(map[string]bool),
|
||
|
Styles: []string{"style1"},
|
||
|
Scripts: []string{"js1"},
|
||
|
}
|
||
|
md.Doc["title"] = "some title"
|
||
|
md.Doc["body"] = "some body"
|
||
|
|
||
|
bufi := new(bytes.Buffer)
|
||
|
bufj := new(bytes.Buffer)
|
||
|
|
||
|
if err := i.Execute(bufi, md); err != nil {
|
||
|
return false, fmt.Sprintf("%v", err), ""
|
||
|
}
|
||
|
if err := j.Execute(bufj, md); err != nil {
|
||
|
return false, "", fmt.Sprintf("%v", err)
|
||
|
}
|
||
|
|
||
|
return bytes.Equal(bufi.Bytes(), bufj.Bytes()), string(bufi.Bytes()), string(bufj.Bytes())
|
||
|
}
|