fix index.html issue (#8)
/index.html will automatic redirect to / refer:https://github.com/golang/go/blob/master/src/net/http/fs.go#L541
This commit is contained in:
parent
1242926197
commit
1d5ab15f82
13
static.go
13
static.go
|
@ -1,14 +1,15 @@
|
||||||
package static
|
package static
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const INDEX = "index.html"
|
||||||
|
|
||||||
type ServeFileSystem interface {
|
type ServeFileSystem interface {
|
||||||
http.FileSystem
|
http.FileSystem
|
||||||
Exists(prefix string, path string) bool
|
Exists(prefix string, path string) bool
|
||||||
|
@ -35,9 +36,15 @@ func (l *localFileSystem) Exists(prefix string, filepath string) bool {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if !l.indexes && stats.IsDir() {
|
if stats.IsDir() {
|
||||||
|
if !l.indexes {
|
||||||
|
index := path.Join(name, INDEX)
|
||||||
|
_, err := os.Stat(index)
|
||||||
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
package static
|
package static
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func performRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
|
func performRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
|
||||||
|
@ -84,3 +84,50 @@ func TestEmptyDirectory(t *testing.T) {
|
||||||
assert.Equal(t, w.Code, 200)
|
assert.Equal(t, w.Code, 200)
|
||||||
assert.Equal(t, w.Body.String(), "Gin Web Framework")
|
assert.Equal(t, w.Body.String(), "Gin Web Framework")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIndex(t *testing.T) {
|
||||||
|
// SETUP file
|
||||||
|
testRoot, _ := os.Getwd()
|
||||||
|
f, err := os.Create(path.Join(testRoot, "index.html"))
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
defer os.Remove(f.Name())
|
||||||
|
f.WriteString("index")
|
||||||
|
f.Close()
|
||||||
|
|
||||||
|
dir, filename := filepath.Split(f.Name())
|
||||||
|
|
||||||
|
router := gin.New()
|
||||||
|
router.Use(ServeRoot("/", dir))
|
||||||
|
|
||||||
|
w := performRequest(router, "GET", "/"+filename)
|
||||||
|
assert.Equal(t, w.Code, 301)
|
||||||
|
|
||||||
|
w = performRequest(router, "GET", "/")
|
||||||
|
assert.Equal(t, w.Code, 200)
|
||||||
|
assert.Equal(t, w.Body.String(), "index")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestListIndex(t *testing.T) {
|
||||||
|
// SETUP file
|
||||||
|
testRoot, _ := os.Getwd()
|
||||||
|
f, err := ioutil.TempFile(testRoot, "")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
defer os.Remove(f.Name())
|
||||||
|
f.WriteString("Gin Web Framework")
|
||||||
|
f.Close()
|
||||||
|
|
||||||
|
dir, filename := filepath.Split(f.Name())
|
||||||
|
router := gin.New()
|
||||||
|
router.Use(Serve("/", LocalFile(dir, true)))
|
||||||
|
|
||||||
|
w := performRequest(router, "GET", "/"+filename)
|
||||||
|
assert.Equal(t, w.Code, 200)
|
||||||
|
assert.Equal(t, w.Body.String(), "Gin Web Framework")
|
||||||
|
|
||||||
|
w = performRequest(router, "GET", "/")
|
||||||
|
assert.Contains(t, w.Body.String(), `<a href="`+filename)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue