2015-07-21 00:58:34 -05:00
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2015-07-21 10:45:32 -05:00
|
|
|
"strings"
|
2015-07-21 00:58:34 -05:00
|
|
|
"testing"
|
|
|
|
|
2015-07-23 02:35:46 -05:00
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-07-21 00:58:34 -05:00
|
|
|
"github.com/russross/blackfriday"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMarkdown(t *testing.T) {
|
|
|
|
templates := make(map[string]string)
|
2015-07-23 02:35:46 -05:00
|
|
|
templates[DefaultTemplate] = "testdata/markdown_tpl.html"
|
2015-07-21 00:58:34 -05:00
|
|
|
md := Markdown{
|
2015-07-23 02:35:46 -05:00
|
|
|
Root: "./testdata",
|
|
|
|
FileSys: http.Dir("./testdata"),
|
2015-07-21 00:58:34 -05:00
|
|
|
Configs: []Config{
|
|
|
|
Config{
|
|
|
|
Renderer: blackfriday.HtmlRenderer(0, "", ""),
|
|
|
|
PathScope: "/blog",
|
2015-07-21 10:45:32 -05:00
|
|
|
Extensions: []string{".md"},
|
2015-07-21 00:58:34 -05:00
|
|
|
Styles: []string{},
|
|
|
|
Scripts: []string{},
|
|
|
|
Templates: templates,
|
|
|
|
},
|
2015-07-21 10:45:32 -05:00
|
|
|
Config{
|
|
|
|
Renderer: blackfriday.HtmlRenderer(0, "", ""),
|
|
|
|
PathScope: "/log",
|
|
|
|
Extensions: []string{".md"},
|
|
|
|
Styles: []string{"/resources/css/log.css", "/resources/css/default.css"},
|
|
|
|
Scripts: []string{"/resources/js/log.js", "/resources/js/default.js"},
|
|
|
|
Templates: make(map[string]string),
|
|
|
|
},
|
2015-07-21 00:58:34 -05:00
|
|
|
},
|
|
|
|
IndexFiles: []string{"index.html"},
|
2015-07-23 02:35:46 -05:00
|
|
|
Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
t.Fatalf("Next shouldn't be called")
|
|
|
|
return 0, nil
|
|
|
|
}),
|
2015-07-21 00:58:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/blog/test.md", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not create HTTP request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
|
|
|
md.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Wrong status, expected: %d and got %d", http.StatusOK, rec.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
respBody := rec.Body.String()
|
|
|
|
expectedBody := `<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Markdown test</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Header</h1>
|
|
|
|
|
|
|
|
Welcome to A Caddy website!
|
|
|
|
<h2>Welcome on the blog</h2>
|
|
|
|
|
|
|
|
<p>Body</p>
|
|
|
|
|
|
|
|
<p><code>go
|
|
|
|
func getTrue() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
</code></p>
|
|
|
|
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`
|
2015-07-21 10:45:32 -05:00
|
|
|
if respBody != expectedBody {
|
|
|
|
t.Fatalf("Expected body: %v got: %v", expectedBody, respBody)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err = http.NewRequest("GET", "/log/test.md", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not create HTTP request: %v", err)
|
|
|
|
}
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
|
|
|
|
md.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
|
|
t.Fatalf("Wrong status, expected: %d and got %d", http.StatusOK, rec.Code)
|
|
|
|
}
|
|
|
|
respBody = rec.Body.String()
|
|
|
|
expectedBody = `<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Markdown test</title>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<link rel="stylesheet" href="/resources/css/log.css">
|
|
|
|
<link rel="stylesheet" href="/resources/css/default.css">
|
|
|
|
|
|
|
|
<script src="/resources/js/log.js"></script>
|
|
|
|
<script src="/resources/js/default.js"></script>
|
|
|
|
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h2>Welcome on the blog</h2>
|
|
|
|
|
|
|
|
<p>Body</p>
|
|
|
|
|
|
|
|
<p><code>go
|
|
|
|
func getTrue() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
</code></p>
|
|
|
|
|
|
|
|
</body>
|
|
|
|
</html>`
|
2015-07-21 00:58:34 -05:00
|
|
|
|
2015-07-21 10:45:32 -05:00
|
|
|
replacer := strings.NewReplacer("\r", "", "\n", "")
|
|
|
|
respBody = replacer.Replace(respBody)
|
|
|
|
expectedBody = replacer.Replace(expectedBody)
|
2015-07-21 00:58:34 -05:00
|
|
|
if respBody != expectedBody {
|
|
|
|
t.Fatalf("Expected body: %v got: %v", expectedBody, respBody)
|
|
|
|
}
|
|
|
|
}
|