1package purell
2
3import (
4	"testing"
5)
6
7// Test cases merged from PR #1
8// Originally from https://github.com/jehiah/urlnorm/blob/master/test_urlnorm.py
9
10func assertMap(t *testing.T, cases map[string]string, f NormalizationFlags) {
11	for bad, good := range cases {
12		s, e := NormalizeURLString(bad, f)
13		if e != nil {
14			t.Errorf("%s normalizing %v to %v", e.Error(), bad, good)
15		} else {
16			if s != good {
17				t.Errorf("source: %v expected: %v got: %v", bad, good, s)
18			}
19		}
20	}
21}
22
23// This tests normalization to a unicode representation
24// precent escapes for unreserved values are unescaped to their unicode value
25// tests normalization to idna domains
26// test ip word handling, ipv6 address handling, and trailing domain periods
27// in general, this matches google chromes unescaping for things in the address bar.
28// spaces are converted to '+' (perhaphs controversial)
29// http://code.google.com/p/google-url/ probably is another good reference for this approach
30func TestUrlnorm(t *testing.T) {
31	testcases := map[string]string{
32		"http://test.example/?a=%e3%82%82%26": "http://test.example/?a=%e3%82%82%26",
33		//"http://test.example/?a=%e3%82%82%26": "http://test.example/?a=\xe3\x82\x82%26", //should return a unicode character
34		"http://s.xn--q-bga.DE/":    "http://s.xn--q-bga.de/",       //should be in idna format
35		"http://XBLA\u306eXbox.com": "http://xn--xblaxbox-jf4g.com", //test utf8 and unicode
36		"http://президент.рф":       "http://xn--d1abbgf6aiiy.xn--p1ai",
37		"http://ПРЕЗИДЕНТ.РФ":       "http://xn--d1abbgf6aiiy.xn--p1ai",
38		"http://ab¥ヲ₩○.com":         "http://xn--ab-ida8983azmfnvs.com", //test width folding
39		"http://\u00e9.com":         "http://xn--9ca.com",
40		"http://e\u0301.com":        "http://xn--9ca.com",
41		"http://ja.wikipedia.org/wiki/%E3%82%AD%E3%83%A3%E3%82%BF%E3%83%94%E3%83%A9%E3%83%BC%E3%82%B8%E3%83%A3%E3%83%91%E3%83%B3": "http://ja.wikipedia.org/wiki/%E3%82%AD%E3%83%A3%E3%82%BF%E3%83%94%E3%83%A9%E3%83%BC%E3%82%B8%E3%83%A3%E3%83%91%E3%83%B3",
42		//"http://ja.wikipedia.org/wiki/%E3%82%AD%E3%83%A3%E3%82%BF%E3%83%94%E3%83%A9%E3%83%BC%E3%82%B8%E3%83%A3%E3%83%91%E3%83%B3": "http://ja.wikipedia.org/wiki/\xe3\x82\xad\xe3\x83\xa3\xe3\x82\xbf\xe3\x83\x94\xe3\x83\xa9\xe3\x83\xbc\xe3\x82\xb8\xe3\x83\xa3\xe3\x83\x91\xe3\x83\xb3",
43
44		"http://test.example/\xe3\x82\xad": "http://test.example/%E3%82%AD",
45		//"http://test.example/\xe3\x82\xad":              "http://test.example/\xe3\x82\xad",
46		"http://test.example/?p=%23val#test-%23-val%25": "http://test.example/?p=%23val#test-%23-val%25", //check that %23 (#) is not escaped where it shouldn't be
47
48		"http://test.domain/I%C3%B1t%C3%ABrn%C3%A2ti%C3%B4n%EF%BF%BDliz%C3%A6ti%C3%B8n": "http://test.domain/I%C3%B1t%C3%ABrn%C3%A2ti%C3%B4n%EF%BF%BDliz%C3%A6ti%C3%B8n",
49		//"http://test.domain/I%C3%B1t%C3%ABrn%C3%A2ti%C3%B4n%EF%BF%BDliz%C3%A6ti%C3%B8n": "http://test.domain/I\xc3\xb1t\xc3\xabrn\xc3\xa2ti\xc3\xb4n\xef\xbf\xbdliz\xc3\xa6ti\xc3\xb8n",
50	}
51
52	assertMap(t, testcases, FlagsSafe|FlagRemoveDotSegments)
53}
54