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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
package domain
import (
"compress/gzip"
"io/ioutil"
"mime"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
"time"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-pages/internal/fixture"
)
func TestGroupServeHTTP(t *testing.T) {
setUpTests()
testGroup := &D{
group: "group",
projectName: "",
}
assert.HTTPBodyContains(t, testGroup.ServeHTTP, "GET", "http://group.test.io/", nil, "main-dir")
assert.HTTPBodyContains(t, testGroup.ServeHTTP, "GET", "http://group.test.io/index.html", nil, "main-dir")
assert.HTTPRedirect(t, testGroup.ServeHTTP, "GET", "http://group.test.io/project", nil)
assert.HTTPBodyContains(t, testGroup.ServeHTTP, "GET", "http://group.test.io/project", nil,
`<a href="//group.test.io/project/">Found</a>`)
assert.HTTPBodyContains(t, testGroup.ServeHTTP, "GET", "http://group.test.io/project/", nil, "project-subdir")
assert.HTTPBodyContains(t, testGroup.ServeHTTP, "GET", "http://group.test.io/project/index.html", nil, "project-subdir")
assert.HTTPRedirect(t, testGroup.ServeHTTP, "GET", "http://group.test.io/project/subdir", nil)
assert.HTTPBodyContains(t, testGroup.ServeHTTP, "GET", "http://group.test.io/project/subdir", nil,
`<a href="//group.test.io/project/subdir/">Found</a>`)
assert.HTTPBodyContains(t, testGroup.ServeHTTP, "GET", "http://group.test.io/project/subdir/", nil, "project-subsubdir")
assert.HTTPBodyContains(t, testGroup.ServeHTTP, "GET", "http://group.test.io/project2/", nil, "project2-main")
assert.HTTPBodyContains(t, testGroup.ServeHTTP, "GET", "http://group.test.io/project2/index.html", nil, "project2-main")
assert.HTTPRedirect(t, testGroup.ServeHTTP, "GET", "http://group.test.io/private.project/", nil)
assert.HTTPError(t, testGroup.ServeHTTP, "GET", "http://group.test.io//about.gitlab.com/%2e%2e", nil)
assert.HTTPError(t, testGroup.ServeHTTP, "GET", "http://group.test.io/symlink", nil)
assert.HTTPError(t, testGroup.ServeHTTP, "GET", "http://group.test.io/symlink/index.html", nil)
assert.HTTPError(t, testGroup.ServeHTTP, "GET", "http://group.test.io/symlink/subdir/", nil)
assert.HTTPError(t, testGroup.ServeHTTP, "GET", "http://group.test.io/project/fifo", nil)
assert.HTTPError(t, testGroup.ServeHTTP, "GET", "http://group.test.io/not-existing-file", nil)
assert.HTTPError(t, testGroup.ServeHTTP, "GET", "http://group.test.io/project//about.gitlab.com/%2e%2e", nil)
}
func TestDomainServeHTTP(t *testing.T) {
setUpTests()
testDomain := &D{
group: "group",
projectName: "project2",
config: &domainConfig{
Domain: "test.domain.com",
},
}
assert.HTTPBodyContains(t, testDomain.ServeHTTP, "GET", "/", nil, "project2-main")
assert.HTTPBodyContains(t, testDomain.ServeHTTP, "GET", "/index.html", nil, "project2-main")
assert.HTTPRedirect(t, testDomain.ServeHTTP, "GET", "/subdir", nil)
assert.HTTPBodyContains(t, testDomain.ServeHTTP, "GET", "/subdir", nil,
`<a href="/subdir/">Found</a>`)
assert.HTTPBodyContains(t, testDomain.ServeHTTP, "GET", "/subdir/", nil, "project2-subdir")
assert.HTTPBodyContains(t, testDomain.ServeHTTP, "GET", "/subdir/index.html", nil, "project2-subdir")
assert.HTTPError(t, testDomain.ServeHTTP, "GET", "//about.gitlab.com/%2e%2e", nil)
assert.HTTPError(t, testDomain.ServeHTTP, "GET", "/not-existing-file", nil)
}
func TestIsHTTPSOnly(t *testing.T) {
tests := []struct {
name string
domain *D
url string
expected bool
}{
{
name: "Custom domain with HTTPS-only enabled",
domain: &D{
group: "group",
projectName: "project",
config: &domainConfig{HTTPSOnly: true},
},
url: "http://custom-domain",
expected: true,
},
{
name: "Custom domain with HTTPS-only disabled",
domain: &D{
group: "group",
projectName: "project",
config: &domainConfig{HTTPSOnly: false},
},
url: "http://custom-domain",
expected: false,
},
{
name: "Default group domain with HTTPS-only enabled",
domain: &D{
group: "group",
projectName: "project",
projects: projects{"test-domain": &project{HTTPSOnly: true}},
},
url: "http://test-domain",
expected: true,
},
{
name: "Default group domain with HTTPS-only disabled",
domain: &D{
group: "group",
projectName: "project",
projects: projects{"test-domain": &project{HTTPSOnly: false}},
},
url: "http://test-domain",
expected: false,
},
{
name: "Case-insensitive default group domain with HTTPS-only enabled",
domain: &D{
group: "group",
projectName: "project",
projects: projects{"test-domain": &project{HTTPSOnly: true}},
},
url: "http://Test-domain",
expected: true,
},
{
name: "Other group domain with HTTPS-only enabled",
domain: &D{
group: "group",
projectName: "project",
projects: projects{"project": &project{HTTPSOnly: true}},
},
url: "http://test-domain/project",
expected: true,
},
{
name: "Other group domain with HTTPS-only disabled",
domain: &D{
group: "group",
projectName: "project",
projects: projects{"project": &project{HTTPSOnly: false}},
},
url: "http://test-domain/project",
expected: false,
},
{
name: "Unknown project",
domain: &D{
group: "group",
projectName: "project",
},
url: "http://test-domain/project",
expected: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, test.url, nil)
assert.Equal(t, test.domain.IsHTTPSOnly(req), test.expected)
})
}
}
func testHTTPGzip(t *testing.T, handler http.HandlerFunc, mode, url string, values url.Values, acceptEncoding string, str interface{}, ungzip bool) {
w := httptest.NewRecorder()
req, err := http.NewRequest(mode, url+"?"+values.Encode(), nil)
require.NoError(t, err)
if acceptEncoding != "" {
req.Header.Add("Accept-Encoding", acceptEncoding)
}
handler(w, req)
if ungzip {
reader, err := gzip.NewReader(w.Body)
require.NoError(t, err)
defer reader.Close()
contentEncoding := w.Header().Get("Content-Encoding")
assert.Equal(t, "gzip", contentEncoding, "Content-Encoding")
bytes, err := ioutil.ReadAll(reader)
require.NoError(t, err)
assert.Contains(t, string(bytes), str)
} else {
assert.Contains(t, w.Body.String(), str)
}
}
func TestGroupServeHTTPGzip(t *testing.T) {
setUpTests()
testGroup := &D{
group: "group",
projectName: "",
}
testSet := []struct {
mode string // HTTP mode
url string // Test URL
params url.Values // Test URL params
acceptEncoding string // Accept encoding header
body interface{} // Expected body at above URL
ungzip bool // Do we expect the request to require unzip?
}{
// No gzip encoding requested
{"GET", "http://group.test.io/", nil, "", "main-dir", false},
{"GET", "http://group.test.io/", nil, "identity", "main-dir", false},
{"GET", "http://group.test.io/", nil, "gzip; q=0", "main-dir", false},
// gzip encoding requeste},
{"GET", "http://group.test.io/", nil, "*", "main-dir", true},
{"GET", "http://group.test.io/", nil, "identity, gzip", "main-dir", true},
{"GET", "http://group.test.io/", nil, "gzip", "main-dir", true},
{"GET", "http://group.test.io/", nil, "gzip; q=1", "main-dir", true},
{"GET", "http://group.test.io/", nil, "gzip; q=0.9", "main-dir", true},
{"GET", "http://group.test.io/", nil, "gzip, deflate", "main-dir", true},
{"GET", "http://group.test.io/", nil, "gzip; q=1, deflate", "main-dir", true},
{"GET", "http://group.test.io/", nil, "gzip; q=0.9, deflate", "main-dir", true},
// gzip encoding requested, but url does not have compressed content on disk
{"GET", "http://group.test.io/project2/", nil, "*", "project2-main", false},
{"GET", "http://group.test.io/project2/", nil, "identity, gzip", "project2-main", false},
{"GET", "http://group.test.io/project2/", nil, "gzip", "project2-main", false},
{"GET", "http://group.test.io/project2/", nil, "gzip; q=1", "project2-main", false},
{"GET", "http://group.test.io/project2/", nil, "gzip; q=0.9", "project2-main", false},
{"GET", "http://group.test.io/project2/", nil, "gzip, deflate", "project2-main", false},
{"GET", "http://group.test.io/project2/", nil, "gzip; q=1, deflate", "project2-main", false},
{"GET", "http://group.test.io/project2/", nil, "gzip; q=0.9, deflate", "project2-main", false},
// malformed headers
{"GET", "http://group.test.io/", nil, ";; gzip", "main-dir", false},
{"GET", "http://group.test.io/", nil, "middle-out", "main-dir", false},
{"GET", "http://group.test.io/", nil, "gzip; quality=1", "main-dir", false},
// Symlinked .gz files are not supported
{"GET", "http://group.test.io/gz-symlink", nil, "*", "data", false},
}
for _, tt := range testSet {
testHTTPGzip(t, testGroup.ServeHTTP, tt.mode, tt.url, tt.params, tt.acceptEncoding, tt.body, tt.ungzip)
}
}
func testHTTP404(t *testing.T, handler http.HandlerFunc, mode, url string, values url.Values, str interface{}) {
w := httptest.NewRecorder()
req, err := http.NewRequest(mode, url+"?"+values.Encode(), nil)
require.NoError(t, err)
handler(w, req)
contentType, _, _ := mime.ParseMediaType(w.Header().Get("Content-Type"))
assert.Equal(t, http.StatusNotFound, w.Code, "HTTP status")
assert.Equal(t, "text/html", contentType, "Content-Type")
assert.Contains(t, w.Body.String(), str)
}
func TestGroup404ServeHTTP(t *testing.T) {
setUpTests()
testGroup := &D{
group: "group.404",
projectName: "",
}
testHTTP404(t, testGroup.ServeHTTP, "GET", "http://group.404.test.io/project.404/not/existing-file", nil, "Custom 404 project page")
testHTTP404(t, testGroup.ServeHTTP, "GET", "http://group.404.test.io/project.404/", nil, "Custom 404 project page")
testHTTP404(t, testGroup.ServeHTTP, "GET", "http://group.404.test.io/project.no.404/not/existing-file", nil, "Custom 404 group page")
testHTTP404(t, testGroup.ServeHTTP, "GET", "http://group.404.test.io/not/existing-file", nil, "Custom 404 group page")
testHTTP404(t, testGroup.ServeHTTP, "GET", "http://group.404.test.io/not-existing-file", nil, "Custom 404 group page")
testHTTP404(t, testGroup.ServeHTTP, "GET", "http://group.404.test.io/", nil, "Custom 404 group page")
assert.HTTPBodyNotContains(t, testGroup.ServeHTTP, "GET", "http://group.404.test.io/project.404.symlink/not/existing-file", nil, "Custom 404 project page")
}
func TestDomain404ServeHTTP(t *testing.T) {
setUpTests()
testDomain := &D{
group: "group.404",
projectName: "domain.404",
config: &domainConfig{
Domain: "domain.404.com",
},
}
testHTTP404(t, testDomain.ServeHTTP, "GET", "http://group.404.test.io/not-existing-file", nil, "Custom 404 group page")
testHTTP404(t, testDomain.ServeHTTP, "GET", "http://group.404.test.io/", nil, "Custom 404 group page")
}
func TestPredefined404ServeHTTP(t *testing.T) {
setUpTests()
testDomain := &D{
group: "group",
}
testHTTP404(t, testDomain.ServeHTTP, "GET", "http://group.test.io/not-existing-file", nil, "The page you're looking for could not be found")
}
func TestGroupCertificate(t *testing.T) {
testGroup := &D{
group: "group",
projectName: "",
}
tls, err := testGroup.EnsureCertificate()
assert.Nil(t, tls)
assert.Error(t, err)
}
func TestDomainNoCertificate(t *testing.T) {
testDomain := &D{
group: "group",
projectName: "project2",
config: &domainConfig{
Domain: "test.domain.com",
},
}
tls, err := testDomain.EnsureCertificate()
assert.Nil(t, tls)
assert.Error(t, err)
_, err2 := testDomain.EnsureCertificate()
assert.Error(t, err)
assert.Equal(t, err, err2)
}
func TestDomainCertificate(t *testing.T) {
testDomain := &D{
group: "group",
projectName: "project2",
config: &domainConfig{
Domain: "test.domain.com",
Certificate: fixture.Certificate,
Key: fixture.Key,
},
}
tls, err := testDomain.EnsureCertificate()
assert.NotNil(t, tls)
require.NoError(t, err)
}
func TestCacheControlHeaders(t *testing.T) {
testGroup := &D{group: "group"}
w := httptest.NewRecorder()
req, err := http.NewRequest("GET", "http://group.test.io/", nil)
require.NoError(t, err)
now := time.Now()
testGroup.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "max-age=600", w.Header().Get("Cache-Control"))
expires := w.Header().Get("Expires")
require.NotEmpty(t, expires)
expiresTime, err := time.Parse(time.RFC1123, expires)
require.NoError(t, err)
assert.WithinDuration(t, now.UTC().Add(10*time.Minute), expiresTime.UTC(), time.Minute)
}
var chdirSet = false
func setUpTests() {
if chdirSet {
return
}
err := os.Chdir("../../shared/pages")
if err != nil {
log.WithError(err).Print("chdir")
} else {
chdirSet = true
}
}
|