diff options
author | Tw <[email protected]> | 2017-06-03 22:28:08 +0800 |
---|---|---|
committer | Tw <[email protected]> | 2017-06-25 09:09:21 +0800 |
commit | 47fc35acc0fe84e25d03da8f27f54ac7ec2ed870 (patch) | |
tree | 094a8141fb777b6eb2dd9283cc06adac91785927 /caddyhttp/markdown/markdown_test.go | |
parent | d3fc9f7a9bcf8272bdd0c022c149db5501ba4f92 (diff) | |
download | caddy-47fc35acc0fe84e25d03da8f27f54ac7ec2ed870.tar.gz caddy-47fc35acc0fe84e25d03da8f27f54ac7ec2ed870.zip |
markdown: fix fake tests
Signed-off-by: Tw <[email protected]>
Diffstat (limited to 'caddyhttp/markdown/markdown_test.go')
-rw-r--r-- | caddyhttp/markdown/markdown_test.go | 112 |
1 files changed, 29 insertions, 83 deletions
diff --git a/caddyhttp/markdown/markdown_test.go b/caddyhttp/markdown/markdown_test.go index 4da39a7f5..658da51e9 100644 --- a/caddyhttp/markdown/markdown_test.go +++ b/caddyhttp/markdown/markdown_test.go @@ -1,16 +1,12 @@ package markdown import ( - "bufio" "io/ioutil" "net/http" "net/http/httptest" - "os" "path/filepath" - "strings" "testing" "text/template" - "time" "github.com/mholt/caddy/caddyhttp/httpserver" "github.com/russross/blackfriday" @@ -79,19 +75,23 @@ func TestMarkdown(t *testing.T) { }), } - 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) + get := func(url string) string { + req, err := http.NewRequest("GET", url, nil) + 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() } - respBody := rec.Body.String() + respBody := get("/blog/test.md") expectedBody := `<!DOCTYPE html> <html> <head> @@ -99,7 +99,6 @@ func TestMarkdown(t *testing.T) { </head> <body> <h1>Header for: Markdown test 1</h1> - Welcome to A Caddy website! <h2>Welcome on the blog</h2> @@ -113,46 +112,22 @@ Welcome to A Caddy website! </body> </html> ` - if !equalStrings(respBody, expectedBody) { - 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() + respBody = get("/docflags/test.md") expectedBody = `Doc.var_string hello -Doc.var_bool <no value> -DocFlags.var_string <no value> -DocFlags.var_bool true` - - if !equalStrings(respBody, expectedBody) { - t.Fatalf("Expected body: %v got: %v", expectedBody, respBody) - } +Doc.var_bool true +` - req, err = http.NewRequest("GET", "/log/test.md", nil) - if err != nil { - t.Fatalf("Could not create HTTP request: %v", err) + if respBody != expectedBody { + t.Fatalf("Expected body:\n%q\ngot:\n%q", expectedBody, respBody) } - 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() + respBody = get("/log/test.md") expectedBody = `<!DOCTYPE html> <html> <head> <title>Markdown test 2</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> @@ -171,26 +146,11 @@ DocFlags.var_bool true` </body> </html>` - if !equalStrings(respBody, expectedBody) { - t.Fatalf("Expected body: %v got: %v", expectedBody, respBody) + if respBody != expectedBody { + t.Fatalf("Expected body:\n%q\ngot:\n%q", expectedBody, respBody) } - req, err = http.NewRequest("GET", "/og/first.md", nil) - 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() + respBody = get("/og/first.md") expectedBody = `<!DOCTYPE html> <html> <head> @@ -198,30 +158,16 @@ DocFlags.var_bool true` </head> <body> <h1>Header for: first_post</h1> - Welcome to title! <h1>Test h1</h1> </body> -</html>` - - if !equalStrings(respBody, expectedBody) { - t.Fatalf("Expected body: %v got: %v", expectedBody, respBody) - } -} +</html> +` -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) + if respBody != expectedBody { + t.Fatalf("Expected body:\n%q\ngot:\n%q", expectedBody, respBody) } - return true } func setDefaultTemplate(filename string) *template.Template { |