-
Notifications
You must be signed in to change notification settings - Fork 125
/
static_middleware_test.go
111 lines (90 loc) · 3.03 KB
/
static_middleware_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
package web
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
func TestStaticMiddleware(t *testing.T) {
currentRoot, _ := os.Getwd()
router := New(Context{})
router.Middleware(StaticMiddleware(currentRoot))
router.Get("/action", (*Context).A)
// Make sure we can still hit actions:
rw, req := newTestRequest("GET", "/action")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "context-A", 200)
rw, req = newTestRequest("GET", "/"+testFilename())
router.ServeHTTP(rw, req)
assertResponse(t, rw, strings.TrimSpace(routerSetupBody()), 200)
rw, req = newTestRequest("POST", "/"+testFilename())
router.ServeHTTP(rw, req)
assertResponse(t, rw, "Not Found", 404)
}
// TestStaticMiddlewareIndex will create an assets folder with one nested subfolder. Each folder will have an index.html file.
func TestStaticMiddlewareOptionIndex(t *testing.T) {
// Create two temporary folders:
dirName, err := ioutil.TempDir("", "")
if err != nil {
panic(err.Error())
}
nestedDirName, err := ioutil.TempDir(dirName, "")
if err != nil {
panic(err.Error())
}
// Get the last path segment of the nestedDirName:
_, nestedDirSegment := filepath.Split(nestedDirName)
// Create first index file
indexFilename := filepath.Join(dirName, "index.html")
err = ioutil.WriteFile(indexFilename, []byte("index1"), os.ModePerm)
if err != nil {
panic(err.Error())
}
defer os.Remove(indexFilename)
// Create second index file
nestedIndexFilename := filepath.Join(nestedDirName, "index.html")
err = ioutil.WriteFile(nestedIndexFilename, []byte("index2"), os.ModePerm)
if err != nil {
panic(err.Error())
}
defer os.Remove(nestedIndexFilename)
// Make router. Static middleware rooted at first temp dir
router := New(Context{})
router.Middleware(StaticMiddleware(dirName, StaticOption{IndexFile: "index.html"}))
router.Get("/action", (*Context).A)
// Getting a root index:
rw, req := newTestRequest("GET", "/")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "index1", 200)
// Nested dir
rw, req = newTestRequest("GET", "/"+nestedDirSegment)
router.ServeHTTP(rw, req)
assertResponse(t, rw, "index2", 200)
// Nested dir with trailing slash:
rw, req = newTestRequest("GET", "/"+nestedDirSegment+"/")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "index2", 200)
}
func TestStaticMiddlewareOptionPrefix(t *testing.T) {
currentRoot, _ := os.Getwd()
router := New(Context{})
router.Middleware(StaticMiddleware(currentRoot, StaticOption{Prefix: "/public"}))
router.Get("/action", (*Context).A)
rw, req := newTestRequest("GET", "/action")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "context-A", 200)
rw, req = newTestRequest("GET", "/"+testFilename())
router.ServeHTTP(rw, req)
assertResponse(t, rw, "Not Found", 404)
rw, req = newTestRequest("GET", "/public/"+testFilename())
router.ServeHTTP(rw, req)
assertResponse(t, rw, strings.TrimSpace(routerSetupBody()), 200)
}
func testFilename() string {
return "router_setup.go"
}
func routerSetupBody() string {
fileBytes, _ := ioutil.ReadFile(testFilename())
return string(fileBytes)
}