File: url_test.go

package info (click to toggle)
golang-github-johanneskaufmann-html-to-markdown 2.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,080 kB
  • sloc: makefile: 3
file content (333 lines) | stat: -rw-r--r-- 7,506 bytes parent folder | download
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
package converter

import (
	"testing"
)

func TestDefaultAssembleAbsoluteURL(t *testing.T) {
	runs := []struct {
		desc string

		tagName string
		input   string
		domain  string

		expected string
	}{
		{
			desc:   "with whitespaces around",
			input:  "  example.com  \n  ",
			domain: "",

			expected: "example.com",
		},
		{
			desc: "empty fragment",

			tagName: "a",
			input:   "#",
			domain:  "",

			expected: "#",
		},
		{
			desc: "fragment",

			tagName: "a",
			input:   "#heading",
			domain:  "",

			expected: "#heading",
		},
		{
			desc: "fragment with space",

			tagName: "a",
			input:   "#my heading",
			domain:  "",

			expected: "#my%20heading",
		},
		{
			desc: "no domain",

			tagName: "a",
			input:   "/page.html?key=val#hash",
			domain:  "",

			expected: "/page.html?key=val#hash",
		},
		{
			desc: "with domain",

			tagName: "a",
			input:   "/page.html?key=val#hash",
			domain:  "test.com",

			expected: "http://test.com/page.html?key=val#hash",
		},
		{
			desc: "with http domain",

			tagName: "a",
			input:   "/page.html?key=val#hash",
			domain:  "http://test.com",

			expected: "http://test.com/page.html?key=val#hash",
		},
		{
			desc: "with https domain",

			tagName: "a",
			input:   "/page.html?key=val#hash",
			domain:  "https://test.com",

			expected: "https://test.com/page.html?key=val#hash",
		},
		{
			desc: "with domain that includes path",

			tagName: "a",
			input:   "/page.html?key=val#hash",
			domain:  "https://test.com/random_stuff",

			expected: "https://test.com/page.html?key=val#hash",
		},

		{
			desc: "data uri",

			tagName: "a",
			input:   "data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7",
			domain:  "test.com",

			expected: "data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7",
		},
		{
			desc: "data uri (with spaces)",

			tagName: "a",
			input:   "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 56 56' width='56' height='56' %3E%3C/svg%3E",
			domain:  "test.com",

			expected: "data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2056%2056'%20width='56'%20height='56'%20%3E%3C/svg%3E",
		},
		{
			desc: "URI scheme",

			tagName: "a",
			input:   "slack://open?team=abc",
			domain:  "test.com",

			expected: "slack://open?team=abc",
		},

		{
			desc: "already with http",

			tagName: "a",
			input:   "http://www.example.com",
			domain:  "test.com",

			expected: "http://www.example.com",
		},
		{
			desc: "already with https",

			tagName: "a",
			input:   "https://www.example.com",
			domain:  "test.com",

			expected: "https://www.example.com",
		},
		{
			desc:   "query parameters",
			input:  "https://www.example.com?a=1&c=2&b=3&x=&y",
			domain: "test.com",

			// Note: If we were to use Query().Encode() the query parameters
			// would be re-ordered as "?a=1&b=3&c=2".
			// We want to keep the original order!
			expected: "https://www.example.com?a=1&c=2&b=3&x=&y",
		},

		{
			desc:   "invalid url with space",
			input:  "https://Open Demo",
			domain: "",

			expected: "https://Open%20Demo",
		},
		{
			desc:   "invalid url with space and brackets",
			input:  "https://Open [foo](uri) Demo",
			domain: "",

			expected: "https://Open%20%5Bfoo%5D%28uri%29%20Demo",
		},

		{
			desc: "mailto",

			tagName: "a",
			input:   "mailto:hi@example.com?subject=Mail&cc=someoneelse@example.com",
			domain:  "test.com",

			expected: "mailto:hi@example.com?subject=Mail&cc=someoneelse%40example.com",
		},
		{
			desc: "invalid url with newline in mailto",

			tagName: "a",
			input:   "mailto:hi@example.com?body=Hello\nJohannes",
			domain:  "test.com",

			expected: "mailto:hi@example.com?body=Hello%0AJohannes",
		},
		{
			desc: "mailto with already encoded space",

			tagName: "a",
			input:   "mailto:hi@example.com?subject=Hello%20Johannes",
			domain:  "test.com",

			expected: "mailto:hi@example.com?subject=Hello%20Johannes",
		},
		{
			desc: "mailto with raw space",

			tagName: "a",
			input:   "mailto:hi@example.com?subject=Greetings to Johannes",
			domain:  "test.com",

			expected: "mailto:hi@example.com?subject=Greetings%20to%20Johannes",
		},
		{
			desc: "mailto with german 'ä' character",

			tagName: "a",
			input:   "mailto:hi@example.com?subject=Sie können gern einen Screenshot anhängen",
			domain:  "test.com",

			// Note: While a space " " is allowed inside then <a> href attribute,
			// in markdown the space would cause the link to not be recognized.
			expected: "mailto:hi@example.com?subject=Sie%20k%C3%B6nnen%20gern%20einen%20Screenshot%20anh%C3%A4ngen",
		},
		{
			desc: "mailto with link",

			tagName: "a",
			input:   "mailto:hi@example.com?body=Article: www.website.com/page.html",
			domain:  "test.com",

			expected: "mailto:hi@example.com?body=Article%3A%20www.website.com%2Fpage.html",
		},
		{
			desc: "brackets inside link #1",

			tagName: "a",
			input:   "foo(and(bar)",
			domain:  "",

			expected: "foo%28and%28bar%29",
		},
		{
			desc: "brackets inside link #2",

			tagName: "a",
			input:   "[foo](uri)",
			domain:  "",

			expected: "%5Bfoo%5D%28uri%29",
		},
	}
	for _, run := range runs {
		t.Run(run.desc, func(t *testing.T) {
			res := defaultAssembleAbsoluteURL(run.tagName, run.input, run.domain)
			if res != run.expected {
				t.Errorf("expected '%s' but got '%s'", run.expected, res)
			}
		})
	}
}

func TestParseAndEncodeQuery(t *testing.T) {
	runs := []struct {
		desc string

		input string

		expected string
	}{
		{
			desc:     "empty string",
			input:    "",
			expected: "",
		},
		{
			desc:     "one pair",
			input:    "a=1",
			expected: "a=1",
		},
		{
			desc:     "multiple pairs",
			input:    "a=1&b=2&c=3",
			expected: "a=1&b=2&c=3",
		},
		{
			desc:     "keep order of multiple pairs",
			input:    "a=1&c=2&b=3",
			expected: "a=1&c=2&b=3",
		},
		{
			desc:     "encode a space",
			input:    "a=hello world&b=hello",
			expected: "a=hello+world&b=hello",
		},

		{
			desc:     "value with space is encoded with percent",
			input:    "key=%20",
			expected: "key=+",
		},
		{
			desc:     "key with space is encoded with percent",
			input:    "%20=value",
			expected: "+=value",
		},
		{
			desc:     "key with space is encoded with plus",
			input:    "key=+",
			expected: "key=+",
		},
		{
			desc:     "value with space is encoded with plus",
			input:    "+=value",
			expected: "+=value",
		},

		{
			desc: "continue on error at value",
			// The error would be:
			//    invalid URL escape "%"
			input:    "a=1&b=%&c=hello world",
			expected: "a=1&b=%&c=hello+world",
		},
		{
			desc: "continue on error at key",
			// The error would be:
			//    invalid URL escape "%"
			input:    "a=1&%=2&c=hello world",
			expected: "a=1&%=2&c=hello+world",
		},
	}

	for _, run := range runs {
		t.Run(run.desc, func(t *testing.T) {
			output := ParseAndEncodeQuery(run.input)
			if output != run.expected {
				t.Errorf("expected '%s' but got '%s'", run.expected, output)
			}
		})
	}
}