forked from gin-contrib/static
-
Notifications
You must be signed in to change notification settings - Fork 1
/
embed_folder_test.go
189 lines (163 loc) · 4.97 KB
/
embed_folder_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package static_test
import (
"embed"
"fmt"
"log"
"net/http"
"testing"
"github.com/gin-gonic/gin"
static "github.com/soulteary/gin-static"
"github.com/stretchr/testify/assert"
)
//go:embed test/data/server
var testFS embed.FS
func TestEmbedFolderWithRedir(t *testing.T) {
tests := []struct {
targetURL string // input
httpCode int // expected http code
httpBody string // expected http body
name string // test name
}{
{"/404.html", 301, "<a href=\"/\">Moved Permanently</a>.\n\n", "Unknown file"},
{"/", 200, "<h1>Hello Embed</h1>", "Root"},
{"/index.html", 301, "", "Root by file name automatic redirect"},
{"/static.html", 200, "<h1>Hello Gin Static</h1>", "Other file"},
}
router := gin.New()
staticFiles, err := static.EmbedFolder(testFS, "test/data/server")
if err != nil {
log.Fatalln("initialization of embed folder failed:", err)
} else {
router.Use(static.Serve("/", staticFiles))
}
router.NoRoute(func(c *gin.Context) {
fmt.Printf("%s doesn't exists, redirect on /\n", c.Request.URL.Path)
c.Redirect(301, "/")
})
for _, tt := range tests {
w := PerformRequest(router, "GET", tt.targetURL)
assert.Equal(t, tt.httpCode, w.Code, tt.name)
assert.Equal(t, tt.httpBody, w.Body.String(), tt.name)
}
}
func TestEmbedFolderWithoutRedir(t *testing.T) {
tests := []struct {
targetURL string // input
httpCode int // expected http code
httpBody string // expected http body
name string // test name
}{
{"/404.html", 404, "404 page not found", "Unknown file"},
{"/", 200, "<h1>Hello Embed</h1>", "Root"},
{"/index.html", 301, "", "Root by file name automatic redirect"},
{"/static.html", 200, "<h1>Hello Gin Static</h1>", "Other file"},
}
router := gin.New()
staticFiles, err := static.EmbedFolder(testFS, "test/data/server")
if err != nil {
log.Fatalln("initialization of embed folder failed:", err)
} else {
router.Use(static.Serve("/", staticFiles))
}
for _, tt := range tests {
w := PerformRequest(router, "GET", tt.targetURL)
assert.Equal(t, tt.httpCode, w.Code, tt.name)
assert.Equal(t, tt.httpBody, w.Body.String(), tt.name)
}
}
func TestEmbedInitErrorPath(t *testing.T) {
tests := []struct {
name string
targetPath string
haveErr bool
fs embed.FS
}{
{
name: "ValidPath",
targetPath: "test/data/server",
haveErr: false,
fs: testFS,
},
{
name: "InvalidPath",
targetPath: "nonexistingdirectory/nonexistingdirectory",
haveErr: true,
fs: testFS,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := static.EmbedFolder(tt.fs, tt.targetPath)
assert.Equal(t, (err != nil), tt.haveErr, tt.name)
})
}
}
func TestCreateEmbed(t *testing.T) {
_, err := static.EmbedFolder(testFS, "test/data/server")
if err != nil {
log.Fatalln("initialization of embed folder failed:", err)
}
_, err2 := static.EmbedFolder(testFS, "")
if err2 != nil {
log.Fatalln("initialization of embed folder failed:", err)
}
}
func TestServEmbed(t *testing.T) {
tests := []struct {
targetURL string // input
httpCode int // expected http code
httpBody string // expected http body
name string // test name
}{
{"/404.html", 404, "404 page not found\n", "Unknown file"},
{"/", 200, "<h1>Hello Embed</h1>", "Root"},
{"/index.html", 301, "<a href=\"/\">Moved Permanently</a>.\n\n", "Root by file name automatic redirect"},
{"/static.html", 200, "<h1>Hello Gin Static</h1>", "Other file"},
}
router := gin.New()
router.Use(static.ServeEmbed("test/data/server", testFS))
router.NoRoute(func(c *gin.Context) {
fmt.Printf("%s doesn't exists, redirect on /\n", c.Request.URL.Path)
c.Redirect(301, "/")
})
for _, tt := range tests {
w := PerformRequest(router, "GET", tt.targetURL)
assert.Equal(t, tt.httpCode, w.Code, tt.name)
assert.Equal(t, tt.httpBody, w.Body.String(), tt.name)
}
router2 := gin.New()
router2.Use(static.ServeEmbed("", testFS))
router2.NoRoute(func(c *gin.Context) {
fmt.Printf("%s doesn't exists, redirect on /\n", c.Request.URL.Path)
c.Redirect(301, "/")
})
for _, tt := range tests {
w := PerformRequest(router2, "GET", "/test/data/server"+tt.targetURL)
assert.Equal(t, tt.httpCode, w.Code, tt.name)
assert.Equal(t, tt.httpBody, w.Body.String(), tt.name)
}
}
func TestServEmbedErr(t *testing.T) {
embedDir := "111111test/data/server"
message := fmt.Sprintf("open %s: file does not exist", embedDir)
tests := []struct {
name string
URL string
Code int
Result string
}{
{
name: "Invalid Path",
URL: "/test/data/server/nonexistingdirectory/nonexistingdirectory",
Code: http.StatusInternalServerError,
Result: fmt.Sprintf("{\"error\":\"%s\",\"message\":\"initialization of embed folder failed\"}", message),
},
}
router := gin.New()
router.Use(static.ServeEmbed(embedDir, testFS))
for _, tt := range tests {
w := PerformRequest(router, "GET", tt.URL)
assert.Equal(t, w.Code, tt.Code)
assert.Equal(t, w.Body.String(), tt.Result)
}
}