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
|
package zip
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-pages/internal/config"
"gitlab.com/gitlab-org/gitlab-pages/internal/serving"
"gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers"
)
func TestZip_ServeFileHTTP(t *testing.T) {
testServerURL, cleanup := newZipFileServerURL(t, "group/zip.gitlab.io/public-without-dirs.zip")
defer cleanup()
tests := map[string]struct {
vfsPath string
path string
expectedStatus int
expectedBody string
}{
"accessing /index.html": {
vfsPath: testServerURL + "/public.zip",
path: "/index.html",
expectedStatus: http.StatusOK,
expectedBody: "zip.gitlab.io/project/index.html\n",
},
"accessing /": {
vfsPath: testServerURL + "/public.zip",
path: "/",
expectedStatus: http.StatusOK,
expectedBody: "zip.gitlab.io/project/index.html\n",
},
"accessing without /": {
vfsPath: testServerURL + "/public.zip",
path: "",
expectedStatus: http.StatusFound,
expectedBody: `<a href="//zip.gitlab.io/zip/">Found</a>.`,
},
"accessing archive that is 404": {
vfsPath: testServerURL + "/invalid.zip",
path: "/index.html",
// we expect the status to not be set
expectedStatus: 0,
},
"accessing archive that is 500": {
vfsPath: testServerURL + "/500",
path: "/index.html",
expectedStatus: http.StatusInternalServerError,
},
}
config.Default.Zip = &config.ZipServing{
ExpirationInterval: 10 * time.Second,
CleanupInterval: 5 * time.Second,
RefreshInterval: 5 * time.Second,
OpenTimeout: 5 * time.Second,
}
s := Instance()
err := s.Reconfigure(config.Default)
require.NoError(t, err)
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://zip.gitlab.io/zip"+test.path, nil)
handler := serving.Handler{
Writer: w,
Request: r,
LookupPath: &serving.LookupPath{
Prefix: "/zip/",
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 := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
require.Contains(t, string(body), test.expectedBody)
})
}
}
var chdirSet = false
func newZipFileServerURL(t *testing.T, zipFilePath string) (string, func()) {
t.Helper()
chdir := testhelpers.ChdirInPath(t, "../../../../shared/pages", &chdirSet)
m := http.NewServeMux()
m.HandleFunc("/public.zip", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, zipFilePath)
}))
m.HandleFunc("/500", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
testServer := httptest.NewServer(m)
return testServer.URL, func() {
chdir()
testServer.Close()
}
}
|