Add Content to serve a file from http.FileSystem #62

Merged
lunny merged 1 commits from lunny/serve_content into master 2019-09-08 13:30:18 +00:00
2 changed files with 30 additions and 1 deletions

12
file.go
View File

@ -4,7 +4,17 @@
package tango
import "path/filepath"
import (
"net/http"
"path/filepath"
)
// Content returns a handle to serve a file
func Content(path string, fs http.FileSystem) func(ctx *Context) {
return func(ctx *Context) {
ctx.ServeContent(path, fs)
}
}
// File returns a handle to serve a file
func File(path string) func(ctx *Context) {

View File

@ -87,3 +87,22 @@ func TestFile1(t *testing.T) {
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "hello tango")
}
func TestContent1(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
tg := New()
tg.Get("/test.html", Content("test.html", http.Dir("./public")))
req, err := http.NewRequest("GET", "http://localhost:8000/test.html", nil)
if err != nil {
t.Error(err)
}
tg.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "hello tango")
}