mirror of
https://github.com/caddyserver/caddy.git
synced 2025-02-02 14:17:01 +01:00
markdown: fix fake tests
Signed-off-by: Tw <tw19881113@gmail.com>
This commit is contained in:
parent
d3fc9f7a9b
commit
47fc35acc0
11 changed files with 98 additions and 90 deletions
|
@ -1,16 +1,12 @@
|
||||||
package markdown
|
package markdown
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||||
"github.com/russross/blackfriday"
|
"github.com/russross/blackfriday"
|
||||||
|
@ -79,19 +75,23 @@ func TestMarkdown(t *testing.T) {
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", "/blog/test.md", nil)
|
get := func(url string) string {
|
||||||
if err != nil {
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
t.Fatalf("Could not create HTTP request: %v", err)
|
if err != nil {
|
||||||
|
t.Fatalf("Could not create HTTP request: %v", err)
|
||||||
|
}
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
code, err := md.ServeHTTP(rec, req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if code != http.StatusOK {
|
||||||
|
t.Fatalf("Wrong status, expected: %d and got %d", http.StatusOK, code)
|
||||||
|
}
|
||||||
|
return rec.Body.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
rec := httptest.NewRecorder()
|
respBody := get("/blog/test.md")
|
||||||
|
|
||||||
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>
|
expectedBody := `<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
|
@ -99,7 +99,6 @@ func TestMarkdown(t *testing.T) {
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Header for: Markdown test 1</h1>
|
<h1>Header for: Markdown test 1</h1>
|
||||||
|
|
||||||
Welcome to A Caddy website!
|
Welcome to A Caddy website!
|
||||||
<h2>Welcome on the blog</h2>
|
<h2>Welcome on the blog</h2>
|
||||||
|
|
||||||
|
@ -113,46 +112,22 @@ Welcome to A Caddy website!
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
`
|
`
|
||||||
if !equalStrings(respBody, expectedBody) {
|
respBody = get("/docflags/test.md")
|
||||||
t.Fatalf("Expected body: %v got: %v", expectedBody, respBody)
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err = http.NewRequest("GET", "/docflags/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 = `Doc.var_string hello
|
expectedBody = `Doc.var_string hello
|
||||||
Doc.var_bool <no value>
|
Doc.var_bool true
|
||||||
DocFlags.var_string <no value>
|
`
|
||||||
DocFlags.var_bool true`
|
|
||||||
|
|
||||||
if !equalStrings(respBody, expectedBody) {
|
if respBody != expectedBody {
|
||||||
t.Fatalf("Expected body: %v got: %v", expectedBody, respBody)
|
t.Fatalf("Expected body:\n%q\ngot:\n%q", expectedBody, respBody)
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err = http.NewRequest("GET", "/log/test.md", nil)
|
respBody = get("/log/test.md")
|
||||||
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>
|
expectedBody = `<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Markdown test 2</title>
|
<title>Markdown test 2</title>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
|
|
||||||
<link rel="stylesheet" href="/resources/css/log.css">
|
<link rel="stylesheet" href="/resources/css/log.css">
|
||||||
<link rel="stylesheet" href="/resources/css/default.css">
|
<link rel="stylesheet" href="/resources/css/default.css">
|
||||||
<script src="/resources/js/log.js"></script>
|
<script src="/resources/js/log.js"></script>
|
||||||
|
@ -171,26 +146,11 @@ DocFlags.var_bool true`
|
||||||
</body>
|
</body>
|
||||||
</html>`
|
</html>`
|
||||||
|
|
||||||
if !equalStrings(respBody, expectedBody) {
|
if respBody != expectedBody {
|
||||||
t.Fatalf("Expected body: %v got: %v", expectedBody, respBody)
|
t.Fatalf("Expected body:\n%q\ngot:\n%q", expectedBody, respBody)
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err = http.NewRequest("GET", "/og/first.md", nil)
|
respBody = get("/og/first.md")
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Could not create HTTP request: %v", err)
|
|
||||||
}
|
|
||||||
rec = httptest.NewRecorder()
|
|
||||||
currenttime := time.Now().Local().Add(-time.Second)
|
|
||||||
_ = os.Chtimes("testdata/og/first.md", currenttime, currenttime)
|
|
||||||
currenttime = time.Now().Local()
|
|
||||||
_ = os.Chtimes("testdata/og_static/og/first.md/index.html", currenttime, currenttime)
|
|
||||||
time.Sleep(time.Millisecond * 200)
|
|
||||||
|
|
||||||
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>
|
expectedBody = `<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
|
@ -198,32 +158,18 @@ DocFlags.var_bool true`
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Header for: first_post</h1>
|
<h1>Header for: first_post</h1>
|
||||||
|
|
||||||
Welcome to title!
|
Welcome to title!
|
||||||
<h1>Test h1</h1>
|
<h1>Test h1</h1>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>`
|
</html>
|
||||||
|
`
|
||||||
|
|
||||||
if !equalStrings(respBody, expectedBody) {
|
if respBody != expectedBody {
|
||||||
t.Fatalf("Expected body: %v got: %v", expectedBody, respBody)
|
t.Fatalf("Expected body:\n%q\ngot:\n%q", expectedBody, respBody)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func equalStrings(s1, s2 string) bool {
|
|
||||||
s1 = strings.TrimSpace(s1)
|
|
||||||
s2 = strings.TrimSpace(s2)
|
|
||||||
in := bufio.NewScanner(strings.NewReader(s1))
|
|
||||||
for in.Scan() {
|
|
||||||
txt := strings.TrimSpace(in.Text())
|
|
||||||
if !strings.HasPrefix(strings.TrimSpace(s2), txt) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
s2 = strings.Replace(s2, txt, "", 1)
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func setDefaultTemplate(filename string) *template.Template {
|
func setDefaultTemplate(filename string) *template.Template {
|
||||||
buf, err := ioutil.ReadFile(filename)
|
buf, err := ioutil.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -115,15 +115,17 @@ func loadParams(c *caddy.Controller, mdc *Config) error {
|
||||||
fpath := filepath.ToSlash(filepath.Clean(cfg.Root + string(filepath.Separator) + tArgs[0]))
|
fpath := filepath.ToSlash(filepath.Clean(cfg.Root + string(filepath.Separator) + tArgs[0]))
|
||||||
|
|
||||||
if err := SetTemplate(mdc.Template, "", fpath); err != nil {
|
if err := SetTemplate(mdc.Template, "", fpath); err != nil {
|
||||||
c.Errf("default template parse error: %v", err)
|
return c.Errf("default template parse error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
case 2:
|
case 2:
|
||||||
fpath := filepath.ToSlash(filepath.Clean(cfg.Root + string(filepath.Separator) + tArgs[1]))
|
fpath := filepath.ToSlash(filepath.Clean(cfg.Root + string(filepath.Separator) + tArgs[1]))
|
||||||
|
|
||||||
if err := SetTemplate(mdc.Template, tArgs[0], fpath); err != nil {
|
if err := SetTemplate(mdc.Template, tArgs[0], fpath); err != nil {
|
||||||
c.Errf("template parse error: %v", err)
|
return c.Errf("template parse error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
case "templatedir":
|
case "templatedir":
|
||||||
|
@ -132,11 +134,12 @@ func loadParams(c *caddy.Controller, mdc *Config) error {
|
||||||
}
|
}
|
||||||
_, err := mdc.Template.ParseGlob(c.Val())
|
_, err := mdc.Template.ParseGlob(c.Val())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Errf("template load error: %v", err)
|
return c.Errf("template load error: %v", err)
|
||||||
}
|
}
|
||||||
if c.NextArg() {
|
if c.NextArg() {
|
||||||
return c.ArgErr()
|
return c.ArgErr()
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
return c.Err("Expected valid markdown configuration property")
|
return c.Err("Expected valid markdown configuration property")
|
||||||
|
|
|
@ -71,12 +71,9 @@ func TestMarkdownParse(t *testing.T) {
|
||||||
Extensions: map[string]struct{}{
|
Extensions: map[string]struct{}{
|
||||||
".md": {},
|
".md": {},
|
||||||
},
|
},
|
||||||
Template: GetDefaultTemplate(),
|
Template: setDefaultTemplate("./testdata/tpl_with_include.html"),
|
||||||
}}},
|
}}},
|
||||||
}
|
}
|
||||||
// Setup the extra template
|
|
||||||
tmpl := tests[1].expectedMarkdownConfig[0].Template
|
|
||||||
SetTemplate(tmpl, "", "./testdata/tpl_with_include.html")
|
|
||||||
|
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
c := caddy.NewTestController("http", test.inputMarkdownConfig)
|
c := caddy.NewTestController("http", test.inputMarkdownConfig)
|
||||||
|
|
14
caddyhttp/markdown/testdata/blog/test.md
vendored
Normal file
14
caddyhttp/markdown/testdata/blog/test.md
vendored
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
---
|
||||||
|
title: Markdown test 1
|
||||||
|
sitename: A Caddy website
|
||||||
|
---
|
||||||
|
|
||||||
|
## Welcome on the blog
|
||||||
|
|
||||||
|
Body
|
||||||
|
|
||||||
|
``` go
|
||||||
|
func getTrue() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
```
|
2
caddyhttp/markdown/testdata/docflags/template.txt
vendored
Normal file
2
caddyhttp/markdown/testdata/docflags/template.txt
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
Doc.var_string {{.Doc.var_string}}
|
||||||
|
Doc.var_bool {{.Doc.var_bool}}
|
4
caddyhttp/markdown/testdata/docflags/test.md
vendored
Normal file
4
caddyhttp/markdown/testdata/docflags/test.md
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
---
|
||||||
|
var_string: hello
|
||||||
|
var_bool: true
|
||||||
|
---
|
1
caddyhttp/markdown/testdata/header.html
vendored
Normal file
1
caddyhttp/markdown/testdata/header.html
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
<h1>Header for: {{.Doc.title}}</h1>
|
14
caddyhttp/markdown/testdata/log/test.md
vendored
Normal file
14
caddyhttp/markdown/testdata/log/test.md
vendored
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
---
|
||||||
|
title: Markdown test 2
|
||||||
|
sitename: A Caddy website
|
||||||
|
---
|
||||||
|
|
||||||
|
## Welcome on the blog
|
||||||
|
|
||||||
|
Body
|
||||||
|
|
||||||
|
``` go
|
||||||
|
func getTrue() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
```
|
11
caddyhttp/markdown/testdata/markdown_tpl.html
vendored
Normal file
11
caddyhttp/markdown/testdata/markdown_tpl.html
vendored
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>{{.Doc.title}}</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
{{.Include "header.html"}}
|
||||||
|
Welcome to {{.Doc.sitename}}!
|
||||||
|
{{.Doc.body}}
|
||||||
|
</body>
|
||||||
|
</html>
|
5
caddyhttp/markdown/testdata/og/first.md
vendored
Normal file
5
caddyhttp/markdown/testdata/og/first.md
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
title: first_post
|
||||||
|
sitename: title
|
||||||
|
---
|
||||||
|
# Test h1
|
11
caddyhttp/markdown/testdata/tpl_with_include.html
vendored
Normal file
11
caddyhttp/markdown/testdata/tpl_with_include.html
vendored
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>{{.Doc.title}}</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
Welcome to {{.Doc.sitename}}!
|
||||||
|
<br><br>
|
||||||
|
{{.Doc.body}}
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in a new issue