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

redirects_test.go « acceptance « test - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dd1b689140ceabca25db0578f08159e477a5cd90 (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
package acceptance_test

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"testing"

	"github.com/stretchr/testify/require"

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

func TestDisabledRedirects(t *testing.T) {
	RunPagesProcess(t,
		withListeners([]ListenSpec{httpListener}),
		withEnv([]string{"FF_ENABLE_REDIRECTS=false", redirects.FFEnablePlaceholders + "=true"}),
	)

	// Test that redirects status page is forbidden
	rsp, err := GetPageFromListener(t, httpListener, "group.redirects.gitlab-example.com", "/project-redirects/_redirects")
	require.NoError(t, err)
	defer rsp.Body.Close()

	require.Equal(t, http.StatusForbidden, rsp.StatusCode)

	// Test that redirects are disabled
	rsp, err = GetRedirectPage(t, httpListener, "group.redirects.gitlab-example.com", "/project-redirects/redirect-portal.html")
	require.NoError(t, err)
	defer rsp.Body.Close()

	require.Equal(t, http.StatusNotFound, rsp.StatusCode)
}

func TestRedirectStatusPage(t *testing.T) {
	RunPagesProcess(t,
		withListeners([]ListenSpec{httpListener}),
		withEnv([]string{redirects.FFEnablePlaceholders + "=true"}),
	)

	rsp, err := GetPageFromListener(t, httpListener, "group.redirects.gitlab-example.com", "/project-redirects/_redirects")
	require.NoError(t, err)

	body, err := ioutil.ReadAll(rsp.Body)
	require.NoError(t, err)
	defer rsp.Body.Close()

	require.Contains(t, string(body), "14 rules")
	require.Equal(t, http.StatusOK, rsp.StatusCode)
}

func TestRedirect(t *testing.T) {
	RunPagesProcess(t,
		withListeners([]ListenSpec{httpListener}),
		withEnv([]string{redirects.FFEnablePlaceholders + "=true"}),
	)

	// Test that serving a file still works with redirects enabled
	rsp, err := GetRedirectPage(t, httpListener, "group.redirects.gitlab-example.com", "/project-redirects/index.html")
	require.NoError(t, err)
	defer rsp.Body.Close()

	require.Equal(t, http.StatusOK, rsp.StatusCode)

	tests := []struct {
		host             string
		path             string
		expectedStatus   int
		expectedLocation string
	}{
		// Project domain
		{
			host:             "group.redirects.gitlab-example.com",
			path:             "/project-redirects/redirect-portal.html",
			expectedStatus:   http.StatusFound,
			expectedLocation: "/project-redirects/magic-land.html",
		},
		// Make sure invalid rule does not redirect
		{
			host:             "group.redirects.gitlab-example.com",
			path:             "/project-redirects/goto-domain.html",
			expectedStatus:   http.StatusNotFound,
			expectedLocation: "",
		},
		// Actual file on disk should override any redirects that match
		{
			host:             "group.redirects.gitlab-example.com",
			path:             "/project-redirects/file-override.html",
			expectedStatus:   http.StatusOK,
			expectedLocation: "",
		},
		// Group-level domain
		{
			host:             "group.redirects.gitlab-example.com",
			path:             "/redirect-portal.html",
			expectedStatus:   http.StatusFound,
			expectedLocation: "/magic-land.html",
		},
		// Custom domain
		{
			host:             "redirects.custom-domain.com",
			path:             "/redirect-portal.html",
			expectedStatus:   http.StatusFound,
			expectedLocation: "/magic-land.html",
		},
		// Permanent redirect for splat (*) with replacement (:splat)
		{
			host:             "group.redirects.gitlab-example.com",
			path:             "/project-redirects/jobs/assistant-to-the-regional-manager.html",
			expectedStatus:   http.StatusMovedPermanently,
			expectedLocation: "/project-redirects/careers/assistant-to-the-regional-manager.html",
		},
	}

	for _, tt := range tests {
		t.Run(fmt.Sprintf("%s%s -> %s (%d)", tt.host, tt.path, tt.expectedLocation, tt.expectedStatus), func(t *testing.T) {
			rsp, err := GetRedirectPage(t, httpListener, tt.host, tt.path)
			require.NoError(t, err)
			defer rsp.Body.Close()

			require.Equal(t, tt.expectedLocation, rsp.Header.Get("Location"))
			require.Equal(t, tt.expectedStatus, rsp.StatusCode)
		})
	}
}