Add Content to serve a file from http.FileSystem #62
12
file.go
12
file.go
|
@ -4,7 +4,17 @@
|
||||||
|
|
||||||
package tango
|
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
|
// File returns a handle to serve a file
|
||||||
func File(path string) func(ctx *Context) {
|
func File(path string) func(ctx *Context) {
|
||||||
|
|
19
file_test.go
19
file_test.go
|
@ -87,3 +87,22 @@ func TestFile1(t *testing.T) {
|
||||||
refute(t, len(buff.String()), 0)
|
refute(t, len(buff.String()), 0)
|
||||||
expect(t, buff.String(), "hello tango")
|
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")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user