forked from gin-contrib/static
-
Notifications
You must be signed in to change notification settings - Fork 1
/
local_file_test.go
42 lines (34 loc) · 1.03 KB
/
local_file_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package static_test
import (
"os"
"path/filepath"
"testing"
"github.com/gin-gonic/gin"
static "github.com/soulteary/gin-static"
"github.com/stretchr/testify/assert"
)
func TestLocalFile(t *testing.T) {
// SETUP file
testRoot, _ := os.Getwd()
f, err := os.CreateTemp(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(static.Serve("/", static.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)
w = PerformRequest(router, "GET", "/"+"../"+filename)
assert.Equal(t, w.Code, 404)
assert.Equal(t, w.Body.String(), "404 page not found")
w = PerformRequest(router, "GET", "/"+"\\"+filename)
assert.Equal(t, w.Code, 404)
assert.Equal(t, w.Body.String(), "404 page not found")
}