1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
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) {
c := caddy.NewTestController("http", `markdown /blog`)
err := setup(c)
if err != nil {
t.Errorf("Expected no errors, got: %v", err)
}
mids := httpserver.GetConfig(c).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("http", test.inputMarkdownConfig)
httpserver.GetConfig(c).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]interface{}),
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())
}
|