Browse Source

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

tags/v0.6.2
Lunny Xiao Gitea 3 months ago
parent
commit
60686424bb
2 changed files with 30 additions and 1 deletions
  1. +11
    -1
      file.go
  2. +19
    -0
      file_test.go

+ 11
- 1
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) {


+ 19
- 0
file_test.go 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")
}

Loading…
Cancel
Save