Browse Source

fix static compitable when prefix is /

tags/v0.5.5
Lunny Xiao 2 years ago
parent
commit
369edb4b28
Signed by: lunny <xiaolunwen@gmail.com> GPG Key ID: C3B7C91B632F738A
5 changed files with 83 additions and 4 deletions
  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 View File

@@ -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 View File

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

+ 6
- 1
static.go View File

@@ -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 View File

@@ -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 View File

@@ -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…
Cancel
Save