2024-07-13 19:58:35 +08:00
|
|
|
package fileserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMiddleware(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
path string
|
|
|
|
dir string
|
|
|
|
requestPath string
|
|
|
|
expectedStatus int
|
|
|
|
expectedContent string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Serve static file",
|
|
|
|
path: "/static/",
|
|
|
|
dir: "./testdata",
|
|
|
|
requestPath: "/static/example.txt",
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedContent: "1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Pass through non-matching path",
|
|
|
|
path: "/static/",
|
|
|
|
dir: "./testdata",
|
|
|
|
requestPath: "/other/path",
|
2024-07-18 23:15:03 +08:00
|
|
|
expectedStatus: http.StatusAlreadyReported,
|
2024-07-13 19:58:35 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Directory with trailing slash",
|
|
|
|
path: "/assets",
|
|
|
|
dir: "testdata",
|
|
|
|
requestPath: "/assets/sample.txt",
|
|
|
|
expectedStatus: http.StatusOK,
|
|
|
|
expectedContent: "2",
|
|
|
|
},
|
2024-07-18 23:15:03 +08:00
|
|
|
{
|
|
|
|
name: "Not exist file",
|
|
|
|
path: "/assets",
|
|
|
|
dir: "testdata",
|
|
|
|
requestPath: "/assets/not-exist.txt",
|
|
|
|
expectedStatus: http.StatusAlreadyReported,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Not exist file in root",
|
|
|
|
path: "/",
|
|
|
|
dir: "testdata",
|
|
|
|
requestPath: "/not-exist.txt",
|
|
|
|
expectedStatus: http.StatusAlreadyReported,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "websocket request",
|
|
|
|
path: "/",
|
|
|
|
dir: "testdata",
|
|
|
|
requestPath: "/ws",
|
|
|
|
expectedStatus: http.StatusAlreadyReported,
|
|
|
|
},
|
2024-07-13 19:58:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-07-17 16:21:08 +08:00
|
|
|
middleware := Middleware(tt.path, http.Dir(tt.dir))
|
2024-07-13 19:58:35 +08:00
|
|
|
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-07-18 23:15:03 +08:00
|
|
|
w.WriteHeader(http.StatusAlreadyReported)
|
2024-07-13 19:58:35 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
handlerToTest := middleware(nextHandler)
|
|
|
|
|
2024-07-18 23:15:03 +08:00
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
req := httptest.NewRequest(http.MethodGet, tt.requestPath, nil)
|
|
|
|
rr := httptest.NewRecorder()
|
2024-07-13 19:58:35 +08:00
|
|
|
|
2024-07-18 23:15:03 +08:00
|
|
|
handlerToTest.ServeHTTP(rr, req)
|
2024-07-13 19:58:35 +08:00
|
|
|
|
2024-07-18 23:15:03 +08:00
|
|
|
assert.Equal(t, tt.expectedStatus, rr.Code)
|
|
|
|
if len(tt.expectedContent) > 0 {
|
|
|
|
assert.Equal(t, tt.expectedContent, rr.Body.String())
|
|
|
|
}
|
2024-07-13 19:58:35 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnsureTrailingSlash(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{"path", "path/"},
|
|
|
|
{"path/", "path/"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.input, func(t *testing.T) {
|
|
|
|
result := ensureTrailingSlash(tt.input)
|
|
|
|
assert.Equal(t, tt.expected, result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnsureNoTrailingSlash(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{"path", "path"},
|
|
|
|
{"path/", "path"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.input, func(t *testing.T) {
|
|
|
|
result := ensureNoTrailingSlash(tt.input)
|
|
|
|
assert.Equal(t, tt.expected, result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|