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 := ` + +
+