2021-03-15 21:11:55 +03:00
|
|
|
package static
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLocalFile(t *testing.T) {
|
|
|
|
// SETUP file
|
|
|
|
testRoot, _ := os.Getwd()
|
|
|
|
f, err := ioutil.TempFile(testRoot, "")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(f.Name())
|
2024-02-17 11:26:13 +03:00
|
|
|
_, err = f.WriteString("Gin Web Framework")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2021-03-15 21:11:55 +03:00
|
|
|
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)
|
|
|
|
}
|