-
Notifications
You must be signed in to change notification settings - Fork 41
/
static_test.go
134 lines (110 loc) · 3.16 KB
/
static_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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package static
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path"
"path/filepath"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func performRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
req, _ := http.NewRequest(method, path, nil)
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())
_, _ = f.WriteString("Gin Web Framework")
f.Close()
dir, filename := filepath.Split(f.Name())
router := gin.New()
router.Use(ServeRoot("/", dir))
router.GET("/", func(c *gin.Context) {
c.String(200, "index")
})
router.GET("/a", func(c *gin.Context) {
c.String(200, "a")
})
router.GET("/"+filename, func(c *gin.Context) {
c.String(200, "this is not printed")
})
w := performRequest(router, "GET", "/")
assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Body.String(), "index")
w = performRequest(router, "GET", "/"+filename)
assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Body.String(), "Gin Web Framework")
w = performRequest(router, "GET", "/"+filename+"a")
assert.Equal(t, w.Code, 404)
w = performRequest(router, "GET", "/a")
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) {
c.String(200, "this is printed")
})
w = performRequest(router2, "GET", "/")
assert.Equal(t, w.Code, 404)
w = performRequest(router2, "GET", "/static")
assert.Equal(t, w.Code, 404)
router2.GET("/static", func(c *gin.Context) {
c.String(200, "index")
})
w = performRequest(router2, "GET", "/static")
assert.Equal(t, w.Code, 200)
w = performRequest(router2, "GET", "/"+filename)
assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Body.String(), "this is printed")
w = performRequest(router2, "GET", "/static/"+filename)
assert.Equal(t, w.Code, 200)
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)
}