0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

Added test for the browse directive

Created sample files for the test
This commit is contained in:
Maxime 2015-07-18 11:37:05 +02:00
parent 2fa6e278d2
commit 2d5320c454
5 changed files with 91 additions and 0 deletions

View file

@ -1,9 +1,14 @@
package browse
import (
"net/http"
"net/http/httptest"
"sort"
"testing"
"text/template"
"time"
"github.com/mholt/caddy/middleware"
)
// "sort" package has "IsSorted" function, but no "IsReversed";
@ -94,3 +99,59 @@ func TestSort(t *testing.T) {
t.Errorf("The listing isn't reversed by time: %v", listing.Items)
}
}
func TestBrowseTemplate(t *testing.T) {
tmpl, err := template.ParseFiles("photos.tpl")
if err != nil {
t.Fatalf("An error occured while parsing the template: %v", err)
}
b := Browse{
Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
t.Fatalf("Next shouldn't be called")
return 0, nil
}),
Root: ".",
Configs: []Config{
Config{
PathScope: "/photos",
Template: tmpl,
},
},
}
req, err := http.NewRequest("GET", "/photos/", nil)
if err != nil {
t.Fatalf("Test: Could not create HTTP request: %v", err)
}
rec := httptest.NewRecorder()
b.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("Wrong status, expected %d, got %d", http.StatusOK, rec.Code)
}
respBody := rec.Body.String()
expectedBody := `<!DOCTYPE html>
<html>
<head>
<title>Template</title>
</head>
<body>
<h1>Header</h1>
<h1>/photos/</h1>
<a href="test.html">test.html</a><br>
<a href="test2.html">test2.html</a><br>
</body>
</html>
`
if respBody != expectedBody {
t.Fatalf("Expected body: %v got: %v", expectedBody, respBody)
}
}

View file

@ -0,0 +1 @@
<h1>Header</h1>

View file

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>Template</title>
</head>
<body>
{{.Include "header.html"}}
<h1>{{.Path}}</h1>
{{range .Items}}
<a href="{{.URL}}">{{.Name}}</a><br>
{{end}}
</body>
</html>

View file

@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
</body>
</html>

View file

@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<title>Test 2</title>
</head>
<body>
</body>
</html>