2016-12-13 05:03:34 +03:00
|
|
|
package static
|
|
|
|
|
|
|
|
import (
|
2022-01-02 09:21:40 +03:00
|
|
|
"context"
|
2016-12-13 05:03:34 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
2018-08-27 05:49:05 +03:00
|
|
|
"path"
|
2016-12-13 05:03:34 +03:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2018-08-27 05:51:13 +03:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2016-12-13 05:03:34 +03:00
|
|
|
)
|
|
|
|
|
2024-02-17 11:09:05 +03:00
|
|
|
//nolint:unparam
|
2021-03-15 21:11:55 +03:00
|
|
|
func PerformRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
|
2022-01-02 09:21:40 +03:00
|
|
|
req, _ := http.NewRequestWithContext(context.Background(), method, path, nil)
|
2016-12-13 05:03:34 +03:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyDirectory(t *testing.T) {
|
|
|
|
// SETUP file
|
|
|
|
testRoot, _ := os.Getwd()
|
|
|
|
f, err := ioutil.TempFile(testRoot, "")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(f.Name())
|
2021-10-02 16:08:24 +03:00
|
|
|
_, _ = f.WriteString("Gin Web Framework")
|
2016-12-13 05:03:34 +03:00
|
|
|
f.Close()
|
|
|
|
|
|
|
|
dir, filename := filepath.Split(f.Name())
|
|
|
|
|
|
|
|
router := gin.New()
|
|
|
|
router.Use(ServeRoot("/", dir))
|
|
|
|
router.GET("/", func(c *gin.Context) {
|
2022-01-02 09:21:40 +03:00
|
|
|
c.String(http.StatusOK, "index")
|
2016-12-13 05:03:34 +03:00
|
|
|
})
|
|
|
|
router.GET("/a", func(c *gin.Context) {
|
2022-01-02 09:21:40 +03:00
|
|
|
c.String(http.StatusOK, "a")
|
2016-12-13 05:03:34 +03:00
|
|
|
})
|
|
|
|
router.GET("/"+filename, func(c *gin.Context) {
|
2022-01-02 09:21:40 +03:00
|
|
|
c.String(http.StatusOK, "this is not printed")
|
2016-12-13 05:03:34 +03:00
|
|
|
})
|
2021-03-15 21:11:55 +03:00
|
|
|
w := PerformRequest(router, "GET", "/")
|
2016-12-13 05:03:34 +03:00
|
|
|
assert.Equal(t, w.Code, 200)
|
|
|
|
assert.Equal(t, w.Body.String(), "index")
|
|
|
|
|
2021-03-15 21:11:55 +03:00
|
|
|
w = PerformRequest(router, "GET", "/"+filename)
|
2016-12-13 05:03:34 +03:00
|
|
|
assert.Equal(t, w.Code, 200)
|
|
|
|
assert.Equal(t, w.Body.String(), "Gin Web Framework")
|
|
|
|
|
2021-03-15 21:11:55 +03:00
|
|
|
w = PerformRequest(router, "GET", "/"+filename+"a")
|
2016-12-13 05:03:34 +03:00
|
|
|
assert.Equal(t, w.Code, 404)
|
|
|
|
|
2021-03-15 21:11:55 +03:00
|
|
|
w = PerformRequest(router, "GET", "/a")
|
2016-12-13 05:03:34 +03:00
|
|
|
assert.Equal(t, w.Code, 200)
|
|
|
|
assert.Equal(t, w.Body.String(), "a")
|
|
|
|
|
|
|
|
router2 := gin.New()
|
|
|
|
router2.Use(ServeRoot("/static", dir))
|
|
|
|
router2.GET("/"+filename, func(c *gin.Context) {
|
2022-01-02 09:21:40 +03:00
|
|
|
c.String(http.StatusOK, "this is printed")
|
2016-12-13 05:03:34 +03:00
|
|
|
})
|
|
|
|
|
2021-03-15 21:11:55 +03:00
|
|
|
w = PerformRequest(router2, "GET", "/")
|
2016-12-13 05:03:34 +03:00
|
|
|
assert.Equal(t, w.Code, 404)
|
|
|
|
|
2021-03-15 21:11:55 +03:00
|
|
|
w = PerformRequest(router2, "GET", "/static")
|
2016-12-13 05:03:34 +03:00
|
|
|
assert.Equal(t, w.Code, 404)
|
|
|
|
router2.GET("/static", func(c *gin.Context) {
|
2022-01-02 09:21:40 +03:00
|
|
|
c.String(http.StatusOK, "index")
|
2016-12-13 05:03:34 +03:00
|
|
|
})
|
|
|
|
|
2021-03-15 21:11:55 +03:00
|
|
|
w = PerformRequest(router2, "GET", "/static")
|
2016-12-13 05:03:34 +03:00
|
|
|
assert.Equal(t, w.Code, 200)
|
|
|
|
|
2021-03-15 21:11:55 +03:00
|
|
|
w = PerformRequest(router2, "GET", "/"+filename)
|
2016-12-13 05:03:34 +03:00
|
|
|
assert.Equal(t, w.Code, 200)
|
|
|
|
assert.Equal(t, w.Body.String(), "this is printed")
|
|
|
|
|
2021-03-15 21:11:55 +03:00
|
|
|
w = PerformRequest(router2, "GET", "/static/"+filename)
|
2016-12-13 05:03:34 +03:00
|
|
|
assert.Equal(t, w.Code, 200)
|
|
|
|
assert.Equal(t, w.Body.String(), "Gin Web Framework")
|
|
|
|
}
|
2018-08-27 05:49:05 +03:00
|
|
|
|
|
|
|
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())
|
2021-10-02 16:08:24 +03:00
|
|
|
_, _ = f.WriteString("index")
|
2018-08-27 05:49:05 +03:00
|
|
|
f.Close()
|
|
|
|
|
|
|
|
dir, filename := filepath.Split(f.Name())
|
|
|
|
|
|
|
|
router := gin.New()
|
|
|
|
router.Use(ServeRoot("/", dir))
|
|
|
|
|
2021-03-15 21:11:55 +03:00
|
|
|
w := PerformRequest(router, "GET", "/"+filename)
|
2018-08-27 05:49:05 +03:00
|
|
|
assert.Equal(t, w.Code, 301)
|
|
|
|
|
2021-03-15 21:11:55 +03:00
|
|
|
w = PerformRequest(router, "GET", "/")
|
2018-08-27 05:49:05 +03:00
|
|
|
assert.Equal(t, w.Code, 200)
|
|
|
|
assert.Equal(t, w.Body.String(), "index")
|
|
|
|
}
|