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
|
package local
import (
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-pages/internal/serving"
"gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers"
)
func TestDisk_ServeFileHTTP(t *testing.T) {
defer setUpTests(t)()
tests := map[string]struct {
vfsPath string
path string
expectedStatus int
expectedBody string
}{
"accessing /index.html": {
vfsPath: "group/serving/public",
path: "/index.html",
expectedStatus: http.StatusOK,
expectedBody: "HTML Document",
},
"accessing /": {
vfsPath: "group/serving/public",
path: "/",
expectedStatus: http.StatusOK,
expectedBody: "HTML Document",
},
"accessing without /": {
vfsPath: "group/serving/public",
path: "",
expectedStatus: http.StatusFound,
expectedBody: `<a href="//group.gitlab-example.com/serving/">Found</a>.`,
},
"accessing vfs path that is missing": {
vfsPath: "group/serving/public-missing",
path: "/index.html",
// we expect the status to not be set
expectedStatus: 0,
},
"accessing vfs path that is forbidden (like file)": {
vfsPath: "group/serving/public/index.html",
path: "/index.html",
expectedStatus: http.StatusInternalServerError,
},
}
s := Instance()
for name, test := range tests {
t.Run(name, func(t *testing.T) {
w := httptest.NewRecorder()
w.Code = 0 // ensure that code is not set, and it is being set by handler
r := httptest.NewRequest("GET", "http://group.gitlab-example.com/serving"+test.path, nil)
handler := serving.Handler{
Writer: w,
Request: r,
LookupPath: &serving.LookupPath{
Prefix: "/serving/",
Path: test.vfsPath,
},
SubPath: test.path,
}
if test.expectedStatus == 0 {
require.False(t, s.ServeFileHTTP(handler))
require.Zero(t, w.Code, "we expect status to not be set")
return
}
require.True(t, s.ServeFileHTTP(handler))
resp := w.Result()
defer resp.Body.Close()
require.Equal(t, test.expectedStatus, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Contains(t, string(body), test.expectedBody)
})
}
}
var chdirSet = false
func setUpTests(t testing.TB) func() {
t.Helper()
return testhelpers.ChdirInPath(t, "../../../../shared/pages", &chdirSet)
}
|