-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathserver_fileserver_test.go
More file actions
202 lines (178 loc) · 6.06 KB
/
server_fileserver_test.go
File metadata and controls
202 lines (178 loc) · 6.06 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
// Zaparoo Core
// Copyright (c) 2026 The Zaparoo Project Contributors.
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of Zaparoo Core.
//
// Zaparoo Core is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Zaparoo Core is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Zaparoo Core. If not, see <http://www.gnu.org/licenses/>.
package api
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"testing/fstest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFsCustom404(t *testing.T) {
t.Parallel()
// Create a mock filesystem with test files
mockFS := fstest.MapFS{
"index.html": {Data: []byte("<!DOCTYPE html><html><body>SPA</body></html>")},
"assets/app.js": {Data: []byte("console.log('app');")},
"assets/style.css": {Data: []byte("body { color: red; }")},
"assets/data.json": {Data: []byte(`{"key": "value"}`)},
"assets/icon.svg": {Data: []byte("<svg></svg>")},
"assets/logo.png": {Data: []byte("PNG binary data")},
"assets/font.woff2": {Data: []byte("WOFF2 binary data")},
}
handler := fsCustom404(http.FS(mockFS))
tests := []struct {
name string
path string
expectedContentType string
expectedBody string
expectedContentTypes []string
expectedStatus int
checkNosniff bool
checkNoCache bool
}{
{
name: "serves index.html at root",
path: "/",
expectedStatus: http.StatusOK,
expectedContentType: "text/html; charset=utf-8",
expectedBody: "<!DOCTYPE html>",
checkNosniff: true,
checkNoCache: true,
},
{
name: "serves JavaScript with correct MIME",
path: "/assets/app.js",
expectedStatus: http.StatusOK,
// MIME type varies by OS: Linux/macOS use text/javascript, Windows uses application/javascript
expectedContentTypes: []string{"text/javascript; charset=utf-8", "application/javascript"},
expectedBody: "console.log",
checkNosniff: true,
},
{
name: "serves CSS with correct MIME",
path: "/assets/style.css",
expectedStatus: http.StatusOK,
expectedContentType: "text/css; charset=utf-8",
expectedBody: "body {",
checkNosniff: true,
},
{
name: "serves JSON with correct MIME",
path: "/assets/data.json",
expectedStatus: http.StatusOK,
expectedContentType: "application/json",
expectedBody: `"key"`,
checkNosniff: true,
},
{
name: "serves SVG with correct MIME",
path: "/assets/icon.svg",
expectedStatus: http.StatusOK,
expectedContentType: "image/svg+xml",
expectedBody: "<svg>",
checkNosniff: true,
},
{
name: "serves WOFF2 with correct MIME",
path: "/assets/font.woff2",
expectedStatus: http.StatusOK,
expectedContentType: "font/woff2",
checkNosniff: true,
},
{
name: "SPA fallback for unknown path",
path: "/some/unknown/route",
expectedStatus: http.StatusOK,
expectedContentType: "text/html; charset=utf-8",
expectedBody: "<!DOCTYPE html>",
checkNosniff: true,
checkNoCache: true,
},
{
name: "SPA fallback for deep nested path",
path: "/app/settings/logs",
expectedStatus: http.StatusOK,
expectedContentType: "text/html; charset=utf-8",
expectedBody: "<!DOCTYPE html>",
checkNosniff: true,
checkNoCache: true,
},
{
name: "directory request serves index",
path: "/assets/",
expectedStatus: http.StatusOK,
expectedContentType: "text/html; charset=utf-8",
checkNosniff: true,
checkNoCache: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, tt.path, http.NoBody)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
assert.Equal(t, tt.expectedStatus, rec.Code)
actualContentType := rec.Header().Get("Content-Type")
if len(tt.expectedContentTypes) > 0 {
assert.Contains(t, tt.expectedContentTypes, actualContentType,
"Content-Type %q not in expected types %v", actualContentType, tt.expectedContentTypes)
} else {
assert.Equal(t, tt.expectedContentType, actualContentType)
}
if tt.expectedBody != "" {
assert.Contains(t, rec.Body.String(), tt.expectedBody)
}
if tt.checkNosniff {
assert.Equal(t, "nosniff", rec.Header().Get("X-Content-Type-Options"))
}
if tt.checkNoCache {
assert.Equal(t, "no-cache", rec.Header().Get("Cache-Control"))
}
})
}
}
func TestFsCustom404_MissingIndex(t *testing.T) {
t.Parallel()
// Filesystem with no index.html
mockFS := fstest.MapFS{
"other.txt": {Data: []byte("not index")},
}
handler := fsCustom404(http.FS(mockFS))
req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/unknown", http.NoBody)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
assert.Equal(t, http.StatusInternalServerError, rec.Code)
}
func TestMimeFallbacks(t *testing.T) {
t.Parallel()
// Verify fallbacks for types Go doesn't have built-in
expectedFallbacks := map[string]string{
".woff": "font/woff",
".woff2": "font/woff2",
}
for ext, expected := range expectedFallbacks {
actual, ok := mimeFallbacks[ext]
require.True(t, ok, "missing fallback for %s", ext)
assert.Equal(t, expected, actual, "wrong fallback for %s", ext)
}
}