Welcome to mirror list, hosted at ThFree Co, Russian Federation.

artifact_test.go « artifact « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6da0e2d99f59cf798b7173d9a75f9c04baa0bd79 (plain)
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
package artifact_test

import (
	"context"
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/stretchr/testify/require"

	"gitlab.com/gitlab-org/gitlab-pages/internal/artifact"
)

func TestTryMakeRequest(t *testing.T) {
	content := "<!DOCTYPE html><html><head><title>Title of the document</title></head><body></body></html>"
	contentType := "text/html; charset=utf-8"

	cases := []struct {
		Path         string
		Token        string
		Status       int
		Content      string
		Length       string
		CacheControl string
		ContentType  string
		Description  string
		RemoteAddr   string
		ForwardedIP  string
	}{
		{
			"/200.html",
			"",
			http.StatusOK,
			content,
			"90",
			"max-age=3600",
			"text/html; charset=utf-8",
			"basic successful request",
			"1.2.3.4:8000",
			"1.2.3.4",
		},
		{
			"/200.html",
			"token",
			http.StatusOK,
			content,
			"90",
			"",
			"text/html; charset=utf-8",
			"basic successful request",
			"1.2.3.4",
			"1.2.3.4",
		},
		{
			"/max-caching.html",
			"",
			http.StatusIMUsed,
			content,
			"90",
			"max-age=3600",
			"text/html; charset=utf-8",
			"max caching request",
			"1.2.3.4",
			"1.2.3.4",
		},
		{
			"/non-caching.html",
			"",
			http.StatusTeapot,
			content,
			"90",
			"",
			"text/html; charset=utf-8",
			"no caching request",
			"1.2.3.4",
			"1.2.3.4",
		},
	}

	for _, c := range cases {
		t.Run(c.Description, func(t *testing.T) {
			testServer := makeArtifactServerStub(t, content, contentType, c.ForwardedIP)
			defer testServer.Close()

			url := "https://group.gitlab-example.io/-/subgroup/project/-/jobs/1/artifacts" + c.Path
			r, err := http.NewRequest(http.MethodGet, url, nil)
			require.NoError(t, err)

			r.RemoteAddr = c.RemoteAddr

			art := artifact.New(testServer.URL, 1, "gitlab-example.io")

			result := httptest.NewRecorder()

			require.True(t, art.TryMakeRequest(result, r, c.Token, func(resp *http.Response) bool { return false }))
			require.Equal(t, c.Status, result.Code)
			require.Equal(t, c.ContentType, result.Header().Get("Content-Type"))
			require.Equal(t, c.Length, result.Header().Get("Content-Length"))
			require.Equal(t, c.CacheControl, result.Header().Get("Cache-Control"))
			require.Equal(t, c.Content, result.Body.String())
		})
	}
}

// provide stub for testing different artifact responses
func makeArtifactServerStub(t *testing.T, content string, contentType string, expectedForwardedIP string) *httptest.Server {
	return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		require.Equal(t, expectedForwardedIP, r.Header.Get("X-Forwarded-For"))

		w.Header().Set("Content-Type", contentType)
		switch r.URL.RawPath {
		case "/projects/group%2Fsubgroup%2Fproject/jobs/1/artifacts/200.html":
			w.WriteHeader(http.StatusOK)
		case "/projects/group%2Fsubgroup%2Fproject/jobs/1/artifacts/max-caching.html":
			w.WriteHeader(http.StatusIMUsed)
		case "/projects/group%2Fsubgroup%2Fproject/jobs/1/artifacts/non-caching.html":
			w.WriteHeader(http.StatusTeapot)
		case "/projects/group%2Fsubgroup%2Fproject/jobs/1/artifacts/500.html":
			w.WriteHeader(http.StatusInternalServerError)
		case "/projects/group%2Fsubgroup%2Fgroup%2Fproject/jobs/1/artifacts/404.html":
			w.WriteHeader(http.StatusNotFound)
		default:
			t.Log("Surprising r.URL.RawPath", r.URL.RawPath)
			w.WriteHeader(999)
		}
		fmt.Fprint(w, content)
	}))
}

func TestBuildURL(t *testing.T) {
	cases := []struct {
		RawServer   string
		Host        string
		Path        string
		Expected    string
		PagesDomain string
		Ok          bool
		Description string
	}{
		{
			"https://gitlab.com/api/v4",
			"group.gitlab.io",
			"/-/project/-/jobs/1/artifacts/",
			`https://gitlab.com/api/v4/projects/group%2Fproject/jobs/1/artifacts/`,
			"gitlab.io",
			true,
			"Basic case",
		},
		{
			"https://gitlab.com/api/v4/",
			"group.gitlab.io",
			"/-/project/-/jobs/1/artifacts/",
			"https://gitlab.com/api/v4/projects/group%2Fproject/jobs/1/artifacts/",
			"gitlab.io",
			true,
			"API URL has trailing slash",
		},
		{
			"https://gitlab.com/api/v4",
			"group.gitlab.io",
			"/-/project/-/jobs/1/artifacts/path/to/file {!#1.txt",
			"https://gitlab.com/api/v4/projects/group%2Fproject/jobs/1/artifacts/path/to/file%20%7B%21%231.txt",
			"gitlab.io",
			true,
			"Special characters in name",
		},
		{
			"https://gitlab.com/api/v4/",
			"GROUP.GITLAB.IO",
			"/-/SUBGROUP/PROJECT/-/JOBS/1/ARTIFACTS/PATH/TO/FILE.txt",
			"https://gitlab.com/api/v4/projects/GROUP%2FSUBGROUP%2FPROJECT/jobs/1/artifacts/PATH/TO/FILE.txt",
			"gitlab.io",
			true,
			"Uppercase names",
		},
		{
			"https://gitlab.com/api/v4",
			"group.gitlab.io",
			"/-/project/-/jobs/1foo1/artifacts/",
			"",
			"gitlab.io",
			false,
			"Job ID has letters",
		},
		{
			"https://gitlab.com/api/v4",
			"group.gitlab.io",
			"/-/project/-/jobs/1$1/artifacts/",
			"",
			"gitlab.io",
			false,
			"Job ID has special characters",
		},
		{
			"https://gitlab.com/api/v4",
			"group.gitlab.io",
			"/-/project/-/jobs/1/artifacts/path/to/file.txt",
			"https://gitlab.com/api/v4/projects/group%2Fproject/jobs/1/artifacts/path/to/file.txt",
			"gitlab.io",
			true,
			"Artifact in subdirectory",
		},
		{
			"https://gitlab.com/api/v4",
			"group.gitlab.io",
			"/-/subgroup1/sub.group2/project/-/jobs/1/artifacts/path/to/file.txt",
			"https://gitlab.com/api/v4/projects/group%2Fsubgroup1%2Fsub.group2%2Fproject/jobs/1/artifacts/path/to/file.txt",
			"gitlab.io",
			true,
			"Basic subgroup case",
		},
		{
			"https://gitlab.com/api/v4",
			"group.gitlab.io",
			"/-//project/-/jobs/1/artifacts/",
			"https://gitlab.com/api/v4/projects/group%2Fproject/jobs/1/artifacts/",
			"gitlab.io",
			true,
			"Leading / in remainder of project path",
		},
		{
			"https://gitlab.com/api/v4",
			"group.gitlab.io",
			"/-/subgroup/project//-/jobs/1/artifacts/",
			"https://gitlab.com/api/v4/projects/group%2Fsubgroup%2Fproject/jobs/1/artifacts/",
			"gitlab.io",
			true,
			"Trailing / in remainder of project path",
		},
		{
			"https://gitlab.com/api/v4",
			"group.gitlab.io",
			"/-//subgroup/project//-/jobs/1/artifacts/",
			"https://gitlab.com/api/v4/projects/group%2Fsubgroup%2Fproject/jobs/1/artifacts/",
			"gitlab.io",
			true,
			"Leading and trailing /",
		},
		{
			"https://gitlab.com/api/v4",
			"group.name.gitlab.io",
			"/-/subgroup/project/-/jobs/1/artifacts/",
			"https://gitlab.com/api/v4/projects/group.name%2Fsubgroup%2Fproject/jobs/1/artifacts/",
			"gitlab.io",
			true,
			"Toplevel group has period",
		},
		{
			"https://gitlab.com/api/v4",
			"gitlab.io.gitlab.io",
			"/-/project/-/jobs/1/artifacts/",
			"https://gitlab.com/api/v4/projects/gitlab.io%2Fproject/jobs/1/artifacts/",
			"gitlab.io",
			true,
			"Toplevel group matches pages domain",
		},
		{
			"https://gitlab.com/api/v4",
			"group.gitlab.io",
			"/-/project/-/jobs/1/artifacts",
			"",
			"gitlab.io",
			false,
			"No artifact specified",
		},
		{
			"https://gitlab.com/api/v4",
			"group.gitlab.io",
			"/index.html",
			"",
			"example.com",
			false,
			"non matching domain and request",
		},
	}

	for _, c := range cases {
		t.Run(c.Description, func(t *testing.T) {
			a := artifact.New(c.RawServer, 1, c.PagesDomain)
			u, ok := a.BuildURL(c.Host, c.Path)

			msg := c.Description + " - generated URL: "
			if u != nil {
				msg = msg + u.String()
			}

			require.Equal(t, c.Ok, ok, msg)
			if c.Ok {
				require.Equal(t, c.Expected, u.String(), c.Description)
			}
		})
	}
}

func TestContextCanceled(t *testing.T) {
	content := "<!DOCTYPE html><html><head><title>Title of the document</title></head><body></body></html>"
	contentType := "text/html; charset=utf-8"
	testServer := makeArtifactServerStub(t, content, contentType, "")
	t.Cleanup(testServer.Close)

	result := httptest.NewRecorder()
	url := "https://group.gitlab-example.io/-/subgroup/project/-/jobs/1/artifacts/200.html"
	r, err := http.NewRequest(http.MethodGet, url, nil)
	require.NoError(t, err)

	ctx, cancel := context.WithCancel(context.Background())
	r = r.WithContext(ctx)
	// cancel context explicitly
	cancel()
	art := artifact.New(testServer.URL, 1, "gitlab-example.io")

	require.True(t, art.TryMakeRequest(result, r, "", func(resp *http.Response) bool { return false }))
	require.Equal(t, http.StatusNotFound, result.Code)
}