github.com/zach-klippenstein/go@v0.0.0-20150108044943-fcfbeb3adf58/src/html/template/url_test.go (about)

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package template
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  func TestURLNormalizer(t *testing.T) {
    12  	tests := []struct {
    13  		url, want string
    14  	}{
    15  		{"", ""},
    16  		{
    17  			"http://example.com:80/foo/bar?q=foo%20&bar=x+y#frag",
    18  			"http://example.com:80/foo/bar?q=foo%20&bar=x+y#frag",
    19  		},
    20  		{" ", "%20"},
    21  		{"%7c", "%7c"},
    22  		{"%7C", "%7C"},
    23  		{"%2", "%252"},
    24  		{"%", "%25"},
    25  		{"%z", "%25z"},
    26  		{"/foo|bar/%5c\u1234", "/foo%7cbar/%5c%e1%88%b4"},
    27  	}
    28  	for _, test := range tests {
    29  		if got := urlNormalizer(test.url); test.want != got {
    30  			t.Errorf("%q: want\n\t%q\nbut got\n\t%q", test.url, test.want, got)
    31  		}
    32  		if test.want != urlNormalizer(test.want) {
    33  			t.Errorf("not idempotent: %q", test.want)
    34  		}
    35  	}
    36  }
    37  
    38  func TestURLFilters(t *testing.T) {
    39  	input := ("\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f" +
    40  		"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" +
    41  		` !"#$%&'()*+,-./` +
    42  		`0123456789:;<=>?` +
    43  		`@ABCDEFGHIJKLMNO` +
    44  		`PQRSTUVWXYZ[\]^_` +
    45  		"`abcdefghijklmno" +
    46  		"pqrstuvwxyz{|}~\x7f" +
    47  		"\u00A0\u0100\u2028\u2029\ufeff\U0001D11E")
    48  
    49  	tests := []struct {
    50  		name    string
    51  		escaper func(...interface{}) string
    52  		escaped string
    53  	}{
    54  		{
    55  			"urlEscaper",
    56  			urlEscaper,
    57  			"%00%01%02%03%04%05%06%07%08%09%0a%0b%0c%0d%0e%0f" +
    58  				"%10%11%12%13%14%15%16%17%18%19%1a%1b%1c%1d%1e%1f" +
    59  				"%20%21%22%23%24%25%26%27%28%29%2a%2b%2c-.%2f" +
    60  				"0123456789%3a%3b%3c%3d%3e%3f" +
    61  				"%40ABCDEFGHIJKLMNO" +
    62  				"PQRSTUVWXYZ%5b%5c%5d%5e_" +
    63  				"%60abcdefghijklmno" +
    64  				"pqrstuvwxyz%7b%7c%7d~%7f" +
    65  				"%c2%a0%c4%80%e2%80%a8%e2%80%a9%ef%bb%bf%f0%9d%84%9e",
    66  		},
    67  		{
    68  			"urlNormalizer",
    69  			urlNormalizer,
    70  			"%00%01%02%03%04%05%06%07%08%09%0a%0b%0c%0d%0e%0f" +
    71  				"%10%11%12%13%14%15%16%17%18%19%1a%1b%1c%1d%1e%1f" +
    72  				"%20!%22#$%25&%27%28%29*+,-./" +
    73  				"0123456789:;%3c=%3e?" +
    74  				"@ABCDEFGHIJKLMNO" +
    75  				"PQRSTUVWXYZ[%5c]%5e_" +
    76  				"%60abcdefghijklmno" +
    77  				"pqrstuvwxyz%7b%7c%7d~%7f" +
    78  				"%c2%a0%c4%80%e2%80%a8%e2%80%a9%ef%bb%bf%f0%9d%84%9e",
    79  		},
    80  	}
    81  
    82  	for _, test := range tests {
    83  		if s := test.escaper(input); s != test.escaped {
    84  			t.Errorf("%s: want\n\t%q\ngot\n\t%q", test.name, test.escaped, s)
    85  			continue
    86  		}
    87  	}
    88  }
    89  
    90  func BenchmarkURLEscaper(b *testing.B) {
    91  	for i := 0; i < b.N; i++ {
    92  		urlEscaper("http://example.com:80/foo?q=bar%20&baz=x+y#frag")
    93  	}
    94  }
    95  
    96  func BenchmarkURLEscaperNoSpecials(b *testing.B) {
    97  	for i := 0; i < b.N; i++ {
    98  		urlEscaper("TheQuickBrownFoxJumpsOverTheLazyDog.")
    99  	}
   100  }
   101  
   102  func BenchmarkURLNormalizer(b *testing.B) {
   103  	for i := 0; i < b.N; i++ {
   104  		urlNormalizer("The quick brown fox jumps over the lazy dog.\n")
   105  	}
   106  }
   107  
   108  func BenchmarkURLNormalizerNoSpecials(b *testing.B) {
   109  	for i := 0; i < b.N; i++ {
   110  		urlNormalizer("http://example.com:80/foo?q=bar%20&baz=x+y#frag")
   111  	}
   112  }