Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
7a697700
Commit
7a697700
authored
Jul 29, 2015
by
Matt Holt
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #207 from evermax/master
markdown: Added test on static files generation
parents
86e9749d
ec51e144
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
181 additions
and
2 deletions
+181
-2
config/setup/markdown_test.go
config/setup/markdown_test.go
+96
-2
config/setup/testdata/blog/first_post.md
config/setup/testdata/blog/first_post.md
+1
-0
config/setup/testdata/header.html
config/setup/testdata/header.html
+1
-0
config/setup/testdata/tpl_with_include.html
config/setup/testdata/tpl_with_include.html
+10
-0
middleware/markdown/markdown_test.go
middleware/markdown/markdown_test.go
+60
-0
middleware/markdown/testdata/og/first.md
middleware/markdown/testdata/og/first.md
+1
-0
middleware/markdown/testdata/og_static/og/first.md/index.html
...leware/markdown/testdata/og_static/og/first.md/index.html
+12
-0
No files found.
config/setup/markdown_test.go
View file @
7a697700
...
...
@@ -2,8 +2,14 @@ package setup
import
(
"fmt"
"github.com/mholt/caddy/middleware/markdown"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"testing"
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware/markdown"
)
func
TestMarkdown
(
t
*
testing
.
T
)
{
...
...
@@ -33,8 +39,82 @@ func TestMarkdown(t *testing.T) {
if
fmt
.
Sprint
(
myHandler
.
Configs
[
0
]
.
Extensions
)
!=
fmt
.
Sprint
([]
string
{
".md"
})
{
t
.
Errorf
(
"Expected .md as the Default Extension"
)
}
}
func
TestMarkdownStaticGen
(
t
*
testing
.
T
)
{
c
:=
NewTestController
(
`markdown /blog {
ext .md
template tpl_with_include.html
sitegen
}`
)
c
.
Root
=
"./testdata"
mid
,
err
:=
Markdown
(
c
)
if
err
!=
nil
{
t
.
Errorf
(
"Expected no errors, got: %v"
,
err
)
}
if
mid
==
nil
{
t
.
Fatal
(
"Expected middleware, was nil instead"
)
}
for
_
,
start
:=
range
c
.
Startup
{
err
:=
start
()
if
err
!=
nil
{
t
.
Errorf
(
"Startup error: %v"
,
err
)
}
}
next
:=
middleware
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
t
.
Fatalf
(
"Next shouldn't be called"
)
return
0
,
nil
})
hndlr
:=
mid
(
next
)
mkdwn
,
ok
:=
hndlr
.
(
markdown
.
Markdown
)
if
!
ok
{
t
.
Fatalf
(
"Was expecting a markdown.Markdown but got %T"
,
hndlr
)
}
expectedStaticFiles
:=
map
[
string
]
string
{
"/blog/first_post.md"
:
"testdata/generated_site/blog/first_post.md/index.html"
}
if
fmt
.
Sprint
(
expectedStaticFiles
)
!=
fmt
.
Sprint
(
mkdwn
.
Configs
[
0
]
.
StaticFiles
)
{
t
.
Fatalf
(
"Test expected StaticFiles to be %s, but got %s"
,
fmt
.
Sprint
(
expectedStaticFiles
),
fmt
.
Sprint
(
mkdwn
.
Configs
[
0
]
.
StaticFiles
))
}
filePath
:=
"testdata/generated_site/blog/first_post.md/index.html"
if
_
,
err
:=
os
.
Stat
(
filePath
);
err
!=
nil
{
t
.
Fatalf
(
"An error occured when getting the file information: %v"
,
err
)
}
html
,
err
:=
ioutil
.
ReadFile
(
filePath
)
if
err
!=
nil
{
t
.
Fatalf
(
"An error occured when getting the file content: %v"
,
err
)
}
expectedBody
:=
`<!DOCTYPE html>
<html>
<head>
<title>first_post</title>
</head>
<body>
<h1>Header title</h1>
<h1>Test h1</h1>
</body>
</html>
`
if
string
(
html
)
!=
expectedBody
{
t
.
Fatalf
(
"Expected file content: %v got: %v"
,
expectedBody
,
html
)
}
fp
:=
filepath
.
Join
(
c
.
Root
,
markdown
.
DefaultStaticDir
)
if
err
=
os
.
RemoveAll
(
fp
);
err
!=
nil
{
t
.
Errorf
(
"Error while removing the generated static files: "
,
err
)
}
}
func
TestMarkdownParse
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
inputMarkdownConfig
string
...
...
@@ -52,9 +132,20 @@ func TestMarkdownParse(t *testing.T) {
Styles
:
[]
string
{
"/resources/css/blog.css"
},
Scripts
:
[]
string
{
"/resources/js/blog.js"
},
}}},
{
`markdown /blog {
ext .md
template tpl_with_include.html
sitegen
}`
,
false
,
[]
markdown
.
Config
{{
PathScope
:
"/blog"
,
Extensions
:
[]
string
{
".md"
},
Templates
:
map
[
string
]
string
{
markdown
.
DefaultTemplate
:
"testdata/tpl_with_include.html"
},
StaticDir
:
markdown
.
DefaultStaticDir
,
}}},
}
for
i
,
test
:=
range
tests
{
c
:=
NewTestController
(
test
.
inputMarkdownConfig
)
c
.
Root
=
"./testdata"
actualMarkdownConfigs
,
err
:=
markdownParse
(
c
)
if
err
==
nil
&&
test
.
shouldErr
{
...
...
@@ -81,7 +172,10 @@ func TestMarkdownParse(t *testing.T) {
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
fmt
.
Sprint
(
actualMarkdownConfig
.
Templates
)
!=
fmt
.
Sprint
(
test
.
expectedMarkdownConfig
[
j
]
.
Templates
)
{
t
.
Errorf
(
"Test %d expected %dth Markdown Config Templates to be %s , but got %s"
,
i
,
j
,
fmt
.
Sprint
(
test
.
expectedMarkdownConfig
[
j
]
.
Templates
),
fmt
.
Sprint
(
actualMarkdownConfig
.
Templates
))
}
}
}
...
...
config/setup/testdata/blog/first_post.md
0 → 100644
View file @
7a697700
# Test h1
config/setup/testdata/header.html
0 → 100644
View file @
7a697700
<h1>
Header title
</h1>
config/setup/testdata/tpl_with_include.html
0 → 100644
View file @
7a697700
<!DOCTYPE html>
<html>
<head>
<title>
{{.Doc.title}}
</title>
</head>
<body>
{{.Include "header.html"}}
{{.Doc.body}}
</body>
</html>
middleware/markdown/markdown_test.go
View file @
7a697700
package
markdown
import
(
"log"
"net/http"
"net/http/httptest"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/mholt/caddy/middleware"
"github.com/russross/blackfriday"
...
...
@@ -38,6 +41,24 @@ func TestMarkdown(t *testing.T) {
StaticDir
:
DefaultStaticDir
,
StaticFiles
:
make
(
map
[
string
]
string
),
},
Config
{
Renderer
:
blackfriday
.
HtmlRenderer
(
0
,
""
,
""
),
PathScope
:
"/og"
,
Extensions
:
[]
string
{
".md"
},
Styles
:
[]
string
{},
Scripts
:
[]
string
{},
Templates
:
templates
,
StaticDir
:
"testdata/og_static"
,
StaticFiles
:
map
[
string
]
string
{
"/og/first.md"
:
"testdata/og_static/og/first.md/index.html"
},
Links
:
[]
PageLink
{
PageLink
{
Title
:
"first"
,
Summary
:
""
,
Date
:
time
.
Now
(),
Url
:
"/og/first.md"
,
},
},
},
},
IndexFiles
:
[]
string
{
"index.html"
},
Next
:
middleware
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
...
...
@@ -129,12 +150,47 @@ func getTrue() bool {
t
.
Fatalf
(
"Expected body: %v got: %v"
,
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
)
err
=
os
.
Chtimes
(
"testdata/og/first.md"
,
currenttime
,
currenttime
)
currenttime
=
time
.
Now
()
.
Local
()
err
=
os
.
Chtimes
(
"testdata/og_static/og/first.md/index.html"
,
currenttime
,
currenttime
)
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>first_post</title>
</head>
<body>
<h1>Header title</h1>
<h1>Test h1</h1>
</body>
</html>`
respBody
=
replacer
.
Replace
(
respBody
)
expectedBody
=
replacer
.
Replace
(
expectedBody
)
if
respBody
!=
expectedBody
{
t
.
Fatalf
(
"Expected body: %v got: %v"
,
expectedBody
,
respBody
)
}
expectedLinks
:=
[]
string
{
"/blog/test.md"
,
"/log/test.md"
,
"/og/first.md"
,
}
for
i
,
c
:=
range
md
.
Configs
{
log
.
Printf
(
"Test number: %d, configuration links: %v, config: %v"
,
i
,
c
.
Links
,
c
)
if
c
.
Links
[
0
]
.
Url
!=
expectedLinks
[
i
]
{
t
.
Fatalf
(
"Expected %v got %v"
,
expectedLinks
[
i
],
c
.
Links
[
0
]
.
Url
)
}
...
...
@@ -158,4 +214,8 @@ func getTrue() bool {
}
w
.
Wait
()
if
err
=
os
.
RemoveAll
(
DefaultStaticDir
);
err
!=
nil
{
t
.
Errorf
(
"Error while removing the generated static files: %v"
,
err
)
}
}
middleware/markdown/testdata/og/first.md
0 → 100644
View file @
7a697700
# Test h1
middleware/markdown/testdata/og_static/og/first.md/index.html
0 → 100644
View file @
7a697700
<!DOCTYPE html>
<html>
<head>
<title>
first_post
</title>
</head>
<body>
<h1>
Header title
</h1>
<h1>
Test h1
</h1>
</body>
</html>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment