瀏覽代碼

fix static compitable when prefix is /

tags/v0.5.5
Lunny Xiao 2 年之前
父節點
當前提交
369edb4b28
簽署人: lunny <xiaolunwen@gmail.com> GPG 金鑰 ID: C3B7C91B632F738A
共有 5 個檔案被更改,包括 83 行新增4 行删除
  1. +20
    -0
      file_test.go
  2. +1
    -0
      public/js/my.js
  3. +6
    -1
      static.go
  4. +55
    -2
      static_test.go
  5. +1
    -1
      tan.go

+ 20
- 0
file_test.go 查看文件

@@ -49,6 +49,26 @@ func TestDir2(t *testing.T) {
expect(t, buff.String(), http.StatusText(http.StatusNotFound))
}

func TestDir3(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff

tg := New()
tg.Get("/*name", Dir("./public"))

req, err := http.NewRequest("GET", "http://localhost:8000/js/my.js", nil)
if err != nil {
t.Error(err)
}

var res = "var test"
tg.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
expect(t, len(buff.String()), len(res))
expect(t, buff.String(), res)
}

func TestFile1(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()


+ 1
- 0
public/js/my.js 查看文件

@@ -0,0 +1 @@
var test

+ 6
- 1
static.go 查看文件

@@ -91,7 +91,12 @@ func Static(opts ...StaticOptions) HandlerFunc {
f, err := opt.FileSystem.Open(strings.TrimLeft(rPath, "/"))
if err != nil {
if os.IsNotExist(err) {
ctx.Result = NotFound()
if opt.Prefix != "" {
ctx.Result = NotFound()
} else {
ctx.Next()
return
}
} else {
ctx.Result = InternalServerError(err.Error())
}


+ 55
- 2
static_test.go 查看文件

@@ -124,7 +124,6 @@ func TestStatic5(t *testing.T) {

tg.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
//expect(t, buff.String(), NotFound().Error())
}

type MemoryFileSystem map[string][]byte
@@ -256,5 +255,59 @@ func TestStatic6(t *testing.T) {

tg.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
//expect(t, buff.String(), NotFound().Error())
}

func TestStatic7(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff

tg := New()
tg.Use(Static(StaticOptions{
RootPath: "./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)
expect(t, buff.String(), "hello tango")
}

func TestStatic8(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff

tg := New()
tg.Use(Return())
tg.Use(Static(StaticOptions{
RootPath: "./public",
}))
tg.Get("/b", func() string {
return "hello"
})

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)
expect(t, buff.String(), "hello tango")

buff.Reset()

req, err = http.NewRequest("GET", "http://localhost:8000/b", nil)
if err != nil {
t.Error(err)
}

tg.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
expect(t, buff.String(), "hello")
}

+ 1
- 1
tan.go 查看文件

@@ -14,7 +14,7 @@ import (

// Version returns tango's version
func Version() string {
return "0.5.4.0328"
return "0.5.4.0517"
}

// Tango describes tango object


Loading…
取消
儲存