diff --git a/middleware/browse/browse_test.go b/middleware/browse/browse_test.go index 33218a19..4c5183b6 100644 --- a/middleware/browse/browse_test.go +++ b/middleware/browse/browse_test.go @@ -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 := ` + + +Template + + +

Header

+ +

/photos/

+ +test.html
+ +test2.html
+ + + +` + + if respBody != expectedBody { + t.Fatalf("Expected body: %v got: %v", expectedBody, respBody) + } +} diff --git a/middleware/browse/header.html b/middleware/browse/header.html new file mode 100644 index 00000000..78e5a6a4 --- /dev/null +++ b/middleware/browse/header.html @@ -0,0 +1 @@ +

Header

diff --git a/middleware/browse/photos.tpl b/middleware/browse/photos.tpl new file mode 100644 index 00000000..5163ca00 --- /dev/null +++ b/middleware/browse/photos.tpl @@ -0,0 +1,13 @@ + + + +Template + + +{{.Include "header.html"}} +

{{.Path}}

+{{range .Items}} +{{.Name}}
+{{end}} + + diff --git a/middleware/browse/photos/test.html b/middleware/browse/photos/test.html new file mode 100644 index 00000000..40535a22 --- /dev/null +++ b/middleware/browse/photos/test.html @@ -0,0 +1,8 @@ + + + +Test + + + + diff --git a/middleware/browse/photos/test2.html b/middleware/browse/photos/test2.html new file mode 100644 index 00000000..8e10c578 --- /dev/null +++ b/middleware/browse/photos/test2.html @@ -0,0 +1,8 @@ + + + +Test 2 + + + +