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

request_test.go « request « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e380c3126b9004c415b1a04bb29fb338a8bb1b8d (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
package request

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

	"github.com/stretchr/testify/require"
)

func TestIsHTTPS(t *testing.T) {
	tests := map[string]struct {
		u      string
		scheme string
	}{
		"when scheme is http": {
			u:      "/",
			scheme: SchemeHTTP,
		},
		"when scheme is https": {
			u:      "/",
			scheme: SchemeHTTPS,
		},
	}
	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			req := httptest.NewRequest(http.MethodGet, test.u, nil)
			req.URL.Scheme = test.scheme

			require.Equal(t, test.scheme == SchemeHTTPS, IsHTTPS(req))
		})
	}
}

func TestGetHostWithoutPort(t *testing.T) {
	tests := map[string]struct {
		u        string
		host     string
		expected string
	}{
		"when port component is provided": {
			u:        "https://example.com:443",
			host:     "my.example.com:8080",
			expected: "my.example.com",
		},
		"when port component is not provided": {
			u:        "http://example.com",
			host:     "my.example.com",
			expected: "my.example.com",
		},
	}
	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			req := httptest.NewRequest(http.MethodGet, test.u, nil)
			req.Host = test.host

			host := GetHostWithoutPort(req)
			require.Equal(t, test.expected, host)
		})
	}
}

func TestGetRemoteAddrWithoutPort(t *testing.T) {
	tests := map[string]struct {
		u          string
		remoteAddr string
		expected   string
	}{
		"when port component is provided": {
			u:          "https://example.com:443",
			remoteAddr: "127.0.0.1:1000",
			expected:   "127.0.0.1",
		},
		"when port component is not provided": {
			u:          "http://example.com",
			remoteAddr: "127.0.0.1",
			expected:   "127.0.0.1",
		},
	}
	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			req := httptest.NewRequest(http.MethodGet, test.u, nil)
			req.RemoteAddr = test.remoteAddr

			addr := GetRemoteAddrWithoutPort(req)
			require.Equal(t, test.expected, addr)
		})
	}
}

func TestGetIPV4orIPV6PrefixWithoutPort(t *testing.T) {
	tests := map[string]struct {
		u          string
		remoteAddr string
		expected   string
	}{
		"when IPv4 and port component is provided": {
			u:          "https://example.com:443",
			remoteAddr: "127.0.0.1:1000",
			expected:   "127.0.0.1",
		},
		"when IPv4 and port component is not provided": {
			u:          "http://example.com",
			remoteAddr: "127.0.0.1",
			expected:   "127.0.0.1",
		},
		"when IPv6 and port component is provided": {
			u:          "https://[2001:db8:3333:4444:5555:6666:7777:8888]:1234",
			remoteAddr: "[2001:db8:3333:4444:5555:6666:7777:8888]:1234",
			expected:   "2001:db8:3333:4444::/64",
		},
		"when IPv6 and port component is not provided": {
			u:          "https://2001:db8:3333:4444:5555:6666:7777:8888",
			remoteAddr: "2001:db8:3333:4444:5555:6666:7777:8888",
			expected:   "2001:db8:3333:4444::/64",
		},
		"when empty remoteAddr is provided": {
			u:          "http://example.com",
			remoteAddr: "",
			expected:   "",
		},
	}
	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			req := httptest.NewRequest(http.MethodGet, test.u, nil)
			req.RemoteAddr = test.remoteAddr

			addr := GetIPV4orIPV6PrefixWithoutPort(req)
			require.Equal(t, test.expected, addr)
		})
	}
}

func TestGetIPV4orIPV6Prefix(t *testing.T) {
	tests := map[string]struct {
		remoteAddr string
		expected   string
	}{
		"when IPv4 and port component is provided": {
			remoteAddr: "127.0.0.1:1000",
			expected:   "127.0.0.1",
		},
		"when IPv4 and port component is not provided": {
			remoteAddr: "127.0.0.1",
			expected:   "127.0.0.1",
		},
		"when IPv6 and port component is provided": {
			remoteAddr: "[2001:db8:3333:4444:5555:6666:7777:8888]:1234",
			expected:   "2001:db8:3333:4444::/64",
		},
		"when IPv6 and port component is not provided": {
			remoteAddr: "2001:db8:3333:4444:5555:6666:7777:8888",
			expected:   "2001:db8:3333:4444::/64",
		},
		"when empty remoteAddr is provided": {
			remoteAddr: "",
			expected:   "",
		},
	}
	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			addr := GetIPV4orIPV6Prefix(test.remoteAddr)
			require.Equal(t, test.expected, addr)
		})
	}
}