aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/aws/smithy-go/endpoints/private/rulesfn/uri_test.go
blob: baa9aaf9363e7b7b312a91f879663f515fbecbaf (plain) (blame)
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
package rulesfn

import (
	"testing"
	"github.com/google/go-cmp/cmp"
)

func TestURIEncode(t *testing.T) {
	cases := map[string]struct {
		input  string
		expect string
	}{
		"no encoding": {
			input:  "a-zA-Z0-9-_.~",
			expect: "a-zA-Z0-9-_.~",
		},
		"with encoding": {
			input:  "🐛 becomes 🦋",
			expect: "%F0%9F%90%9B%20becomes%20%F0%9F%A6%8B",
		},
	}

	for name, c := range cases {
		t.Run(name, func(t *testing.T) {
			actual := URIEncode(c.input)
			if e, a := c.expect, actual; e != a {
				t.Errorf("expect `%v` encoding, got `%v`", e, a)
			}
		})
	}
}

func TestParseURL(t *testing.T) {
	cases := map[string]struct {
		input     string
		expect    *URL
	}{
		"https hostname with no path": {
			input: "https://example.com",
			expect: &URL{
				Scheme:         "https",
				Authority:      "example.com",
				Path:           "",
				NormalizedPath: "/",
			},
		},
		"http hostname with no path": {
			input: "http://example.com",
			expect: &URL{
				Scheme:         "http",
				Authority:      "example.com",
				Path:           "",
				NormalizedPath: "/",
			},
		},
		"https hostname with port with path": {
			input: "https://example.com:80/foo/bar",
			expect: &URL{
				Scheme:         "https",
				Authority:      "example.com:80",
				Path:           "/foo/bar",
				NormalizedPath: "/foo/bar/",
			},
		},
		"invalid port": {
			input:     "https://example.com:abc",
			expect: nil,
		},
		"with query": {
			input:     "https://example.com:8443?foo=bar&faz=baz",
			expect: nil,
		},
		"ip4 URL": {
			input: "https://127.0.0.1",
			expect: &URL{
				Scheme:         "https",
				Authority:      "127.0.0.1",
				Path:           "",
				NormalizedPath: "/",
				IsIp:           true,
			},
		},
		"ip4 URL with port": {
			input: "https://127.0.0.1:8443",
			expect: &URL{
				Scheme:         "https",
				Authority:      "127.0.0.1:8443",
				Path:           "",
				NormalizedPath: "/",
				IsIp:           true,
			},
		},
		"ip6 short": {
			input: "https://[fe80::1]",
			expect: &URL{
				Scheme:         "https",
				Authority:      "[fe80::1]",
				Path:           "",
				NormalizedPath: "/",
				IsIp:           true,
			},
		},
		"ip6 short with interface": {
			input: "https://[fe80::1%25en0]",
			expect: &URL{
				Scheme:         "https",
				Authority:      "[fe80::1%25en0]",
				Path:           "",
				NormalizedPath: "/",
				IsIp:           true,
			},
		},
		"ip6 short with port": {
			input: "https://[fe80::1]:8443",
			expect: &URL{
				Scheme:         "https",
				Authority:      "[fe80::1]:8443",
				Path:           "",
				NormalizedPath: "/",
				IsIp:           true,
			},
		},
		"ip6 short with port with interface": {
			input: "https://[fe80::1%25en0]:8443",
			expect: &URL{
				Scheme:         "https",
				Authority:      "[fe80::1%25en0]:8443",
				Path:           "",
				NormalizedPath: "/",
				IsIp:           true,
			},
		},
	}

	for name, c := range cases {
		t.Run(name, func(t *testing.T) {
			actual := ParseURL(c.input)
			if c.expect == nil {
				if actual != nil {
					t.Fatalf("expect no result, got %v", *actual)
				}
				return
			}

			if actual == nil {
				t.Fatalf("expect result, got none")
			}

			if diff := cmp.Diff(c.expect, actual); diff != "" {
				t.Errorf("expect URL to match\n%s", diff)
			}
		})
	}
}

func TestIsValidHostLabel(t *testing.T) {
	cases := map[string]struct {
		input           string
		allowSubDomains bool
		expect          bool
	}{
		"single label no split": {
			input:  "abc123-",
			expect: true,
		},
		"single label with split": {
			input:           "abc123-",
			allowSubDomains: true,
			expect:          true,
		},
		"multiple labels no split": {
			input:     "abc.123-",
			expect: false,
		},
		"multiple labels with split": {
			input:           "abc.123-",
			allowSubDomains: true,
			expect:          true,
		},
		"multiple labels with split invalid label": {
			input:           "abc.123-...",
			allowSubDomains: true,
			expect: false,
		},
		"max length host label": {
			input:  "012345678901234567890123456789012345678901234567890123456789123",
			expect: true,
		},
		"too large host label": {
			input:     "0123456789012345678901234567890123456789012345678901234567891234",
			expect: false,
		},
		"too small host label": {
			input:     "",
			expect: false,
		},
	}

	for name, c := range cases {
		t.Run(name, func(t *testing.T) {
			actual := IsValidHostLabel(c.input, c.allowSubDomains)
			if e, a := c.expect, actual; e != a {
				t.Fatalf("expect %v valid host label, got %v", e, a)
			}
		})
	}
}