-
Notifications
You must be signed in to change notification settings - Fork 13
/
middleware_static_filesystem_test.go
103 lines (78 loc) · 2.67 KB
/
middleware_static_filesystem_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
package rye
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Static File Middleware", func() {
var (
request *http.Request
response *httptest.ResponseRecorder
path string
testPath string
)
BeforeEach(func() {
response = httptest.NewRecorder()
request = &http.Request{}
testPath, _ = os.Getwd()
})
Describe("handle", func() {
Context("when a valid file is referenced", func() {
It("should return a response", func() {
path = "/static-examples/dist/"
request, _ = http.NewRequest("GET", "/dist/test.html", nil)
resp := NewStaticFilesystem(testPath+path, "/dist/")(response, request)
Expect(resp).To(BeNil())
Expect(response).ToNot(BeNil())
Expect(response.Code).To(Equal(200))
body, err := ioutil.ReadAll(response.Body)
Expect(err).To(BeNil())
Expect(body).To(ContainSubstring("Test.html"))
})
It("should return Index.html when request is just path", func() {
path = "/static-examples/dist/"
request, _ := http.NewRequest("GET", "/dist/", nil)
resp := NewStaticFilesystem(testPath+path, "/dist/")(response, request)
Expect(resp).To(BeNil())
Expect(response).ToNot(BeNil())
Expect(response.Code).To(Equal(200))
body, err := ioutil.ReadAll(response.Body)
Expect(err).To(BeNil())
Expect(body).To(ContainSubstring("Index.html"))
})
It("should return Index.html when strip prefix is empty", func() {
path = "/static-examples/dist/"
request, _ := http.NewRequest("GET", "/", nil)
resp := NewStaticFilesystem(testPath+path, "")(response, request)
Expect(resp).To(BeNil())
Expect(response).ToNot(BeNil())
Expect(response.Code).To(Equal(200))
body, err := ioutil.ReadAll(response.Body)
Expect(err).To(BeNil())
Expect(body).To(ContainSubstring("Index.html"))
})
It("should return Index.html when strip prefix is empty", func() {
path = "/static-examples/dist/"
request, _ := http.NewRequest("GET", "/ASDads.HTML", nil)
resp := NewStaticFilesystem(testPath+path, "")(response, request)
Expect(resp).To(BeNil())
Expect(response).ToNot(BeNil())
Expect(response.Code).To(Equal(404))
})
It("should return test.css on subpath", func() {
path = "/static-examples/dist/"
request, _ := http.NewRequest("GET", "/styles/test.css", nil)
resp := NewStaticFilesystem(testPath+path, "")(response, request)
Expect(resp).To(BeNil())
Expect(response).ToNot(BeNil())
Expect(response.Code).To(Equal(200))
body, err := ioutil.ReadAll(response.Body)
Expect(err).To(BeNil())
Expect(body).To(ContainSubstring("test.css"))
})
})
})
})