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

url_test.go « htmltemplate « go_templates « internal « tpl - github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ff0459ffd13e3b07d315ecccd6dc9233ebe80d50 (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
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build go1.13,!windows

package template

import (
	"testing"
)

func TestURLNormalizer(t *testing.T) {
	tests := []struct {
		url, want string
	}{
		{"", ""},
		{
			"http://example.com:80/foo/bar?q=foo%20&bar=x+y#frag",
			"http://example.com:80/foo/bar?q=foo%20&bar=x+y#frag",
		},
		{" ", "%20"},
		{"%7c", "%7c"},
		{"%7C", "%7C"},
		{"%2", "%252"},
		{"%", "%25"},
		{"%z", "%25z"},
		{"/foo|bar/%5c\u1234", "/foo%7cbar/%5c%e1%88%b4"},
	}
	for _, test := range tests {
		if got := urlNormalizer(test.url); test.want != got {
			t.Errorf("%q: want\n\t%q\nbut got\n\t%q", test.url, test.want, got)
		}
		if test.want != urlNormalizer(test.want) {
			t.Errorf("not idempotent: %q", test.want)
		}
	}
}

func TestURLFilters(t *testing.T) {
	input := ("\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f" +
		"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" +
		` !"#$%&'()*+,-./` +
		`0123456789:;<=>?` +
		`@ABCDEFGHIJKLMNO` +
		`PQRSTUVWXYZ[\]^_` +
		"`abcdefghijklmno" +
		"pqrstuvwxyz{|}~\x7f" +
		"\u00A0\u0100\u2028\u2029\ufeff\U0001D11E")

	tests := []struct {
		name    string
		escaper func(...interface{}) string
		escaped string
	}{
		{
			"urlEscaper",
			urlEscaper,
			"%00%01%02%03%04%05%06%07%08%09%0a%0b%0c%0d%0e%0f" +
				"%10%11%12%13%14%15%16%17%18%19%1a%1b%1c%1d%1e%1f" +
				"%20%21%22%23%24%25%26%27%28%29%2a%2b%2c-.%2f" +
				"0123456789%3a%3b%3c%3d%3e%3f" +
				"%40ABCDEFGHIJKLMNO" +
				"PQRSTUVWXYZ%5b%5c%5d%5e_" +
				"%60abcdefghijklmno" +
				"pqrstuvwxyz%7b%7c%7d~%7f" +
				"%c2%a0%c4%80%e2%80%a8%e2%80%a9%ef%bb%bf%f0%9d%84%9e",
		},
		{
			"urlNormalizer",
			urlNormalizer,
			"%00%01%02%03%04%05%06%07%08%09%0a%0b%0c%0d%0e%0f" +
				"%10%11%12%13%14%15%16%17%18%19%1a%1b%1c%1d%1e%1f" +
				"%20!%22#$%25&%27%28%29*+,-./" +
				"0123456789:;%3c=%3e?" +
				"@ABCDEFGHIJKLMNO" +
				"PQRSTUVWXYZ[%5c]%5e_" +
				"%60abcdefghijklmno" +
				"pqrstuvwxyz%7b%7c%7d~%7f" +
				"%c2%a0%c4%80%e2%80%a8%e2%80%a9%ef%bb%bf%f0%9d%84%9e",
		},
	}

	for _, test := range tests {
		if s := test.escaper(input); s != test.escaped {
			t.Errorf("%s: want\n\t%q\ngot\n\t%q", test.name, test.escaped, s)
			continue
		}
	}
}

func TestSrcsetFilter(t *testing.T) {
	tests := []struct {
		name  string
		input string
		want  string
	}{
		{
			"one ok",
			"http://example.com/img.png",
			"http://example.com/img.png",
		},
		{
			"one ok with metadata",
			" /img.png 200w",
			" /img.png 200w",
		},
		{
			"one bad",
			"javascript:alert(1) 200w",
			"#ZgotmplZ",
		},
		{
			"two ok",
			"foo.png, bar.png",
			"foo.png, bar.png",
		},
		{
			"left bad",
			"javascript:alert(1), /foo.png",
			"#ZgotmplZ, /foo.png",
		},
		{
			"right bad",
			"/bogus#, javascript:alert(1)",
			"/bogus#,#ZgotmplZ",
		},
	}

	for _, test := range tests {
		if got := srcsetFilterAndEscaper(test.input); got != test.want {
			t.Errorf("%s: srcsetFilterAndEscaper(%q) want %q != %q", test.name, test.input, test.want, got)
		}
	}
}

func BenchmarkURLEscaper(b *testing.B) {
	for i := 0; i < b.N; i++ {
		urlEscaper("http://example.com:80/foo?q=bar%20&baz=x+y#frag")
	}
}

func BenchmarkURLEscaperNoSpecials(b *testing.B) {
	for i := 0; i < b.N; i++ {
		urlEscaper("TheQuickBrownFoxJumpsOverTheLazyDog.")
	}
}

func BenchmarkURLNormalizer(b *testing.B) {
	for i := 0; i < b.N; i++ {
		urlNormalizer("The quick brown fox jumps over the lazy dog.\n")
	}
}

func BenchmarkURLNormalizerNoSpecials(b *testing.B) {
	for i := 0; i < b.N; i++ {
		urlNormalizer("http://example.com:80/foo?q=bar%20&baz=x+y#frag")
	}
}

func BenchmarkSrcsetFilter(b *testing.B) {
	for i := 0; i < b.N; i++ {
		srcsetFilterAndEscaper(" /foo/bar.png 200w, /baz/boo(1).png")
	}
}

func BenchmarkSrcsetFilterNoSpecials(b *testing.B) {
	for i := 0; i < b.N; i++ {
		srcsetFilterAndEscaper("http://example.com:80/foo?q=bar%20&baz=x+y#frag")
	}
}