@@ -1754,3 +1754,106 @@ func TestInvalidUserPassword(t *testing.T) {
1754
1754
t .Errorf ("error = %q; want substring %q" , got , wantsub )
1755
1755
}
1756
1756
}
1757
+
1758
+ var escapeBenchmarks = []struct {
1759
+ unescaped string
1760
+ query string
1761
+ path string
1762
+ }{
1763
+ {
1764
+ unescaped : "one two" ,
1765
+ query : "one+two" ,
1766
+ path : "one%20two" ,
1767
+ },
1768
+ {
1769
+ unescaped : "Фотки собак" ,
1770
+ query : "%D0%A4%D0%BE%D1%82%D0%BA%D0%B8+%D1%81%D0%BE%D0%B1%D0%B0%D0%BA" ,
1771
+ path : "%D0%A4%D0%BE%D1%82%D0%BA%D0%B8%20%D1%81%D0%BE%D0%B1%D0%B0%D0%BA" ,
1772
+ },
1773
+
1774
+ {
1775
+ unescaped : "shortrun(break)shortrun" ,
1776
+ query : "shortrun%28break%29shortrun" ,
1777
+ path : "shortrun%28break%29shortrun" ,
1778
+ },
1779
+
1780
+ {
1781
+ unescaped : "longerrunofcharacters(break)anotherlongerrunofcharacters" ,
1782
+ query : "longerrunofcharacters%28break%29anotherlongerrunofcharacters" ,
1783
+ path : "longerrunofcharacters%28break%29anotherlongerrunofcharacters" ,
1784
+ },
1785
+
1786
+ {
1787
+ unescaped : strings .Repeat ("padded/with+various%characters?that=need$some@escaping+paddedsowebreak/256bytes" , 4 ),
1788
+ query : strings .Repeat ("padded%2Fwith%2Bvarious%25characters%3Fthat%3Dneed%24some%40escaping%2Bpaddedsowebreak%2F256bytes" , 4 ),
1789
+ path : strings .Repeat ("padded%2Fwith+various%25characters%3Fthat=need$some@escaping+paddedsowebreak%2F256bytes" , 4 ),
1790
+ },
1791
+ }
1792
+
1793
+ func BenchmarkQueryEscape (b * testing.B ) {
1794
+ for _ , tc := range escapeBenchmarks {
1795
+ b .Run ("" , func (b * testing.B ) {
1796
+ b .ReportAllocs ()
1797
+ var g string
1798
+ for i := 0 ; i < b .N ; i ++ {
1799
+ g = QueryEscape (tc .unescaped )
1800
+ }
1801
+ b .StopTimer ()
1802
+ if g != tc .query {
1803
+ b .Errorf ("QueryEscape(%q) == %q, want %q" , tc .unescaped , g , tc .query )
1804
+ }
1805
+
1806
+ })
1807
+ }
1808
+ }
1809
+
1810
+ func BenchmarkPathEscape (b * testing.B ) {
1811
+ for _ , tc := range escapeBenchmarks {
1812
+ b .Run ("" , func (b * testing.B ) {
1813
+ b .ReportAllocs ()
1814
+ var g string
1815
+ for i := 0 ; i < b .N ; i ++ {
1816
+ g = PathEscape (tc .unescaped )
1817
+ }
1818
+ b .StopTimer ()
1819
+ if g != tc .path {
1820
+ b .Errorf ("PathEscape(%q) == %q, want %q" , tc .unescaped , g , tc .path )
1821
+ }
1822
+
1823
+ })
1824
+ }
1825
+ }
1826
+
1827
+ func BenchmarkQueryUnescape (b * testing.B ) {
1828
+ for _ , tc := range escapeBenchmarks {
1829
+ b .Run ("" , func (b * testing.B ) {
1830
+ b .ReportAllocs ()
1831
+ var g string
1832
+ for i := 0 ; i < b .N ; i ++ {
1833
+ g , _ = QueryUnescape (tc .query )
1834
+ }
1835
+ b .StopTimer ()
1836
+ if g != tc .unescaped {
1837
+ b .Errorf ("QueryUnescape(%q) == %q, want %q" , tc .query , g , tc .unescaped )
1838
+ }
1839
+
1840
+ })
1841
+ }
1842
+ }
1843
+
1844
+ func BenchmarkPathUnescape (b * testing.B ) {
1845
+ for _ , tc := range escapeBenchmarks {
1846
+ b .Run ("" , func (b * testing.B ) {
1847
+ b .ReportAllocs ()
1848
+ var g string
1849
+ for i := 0 ; i < b .N ; i ++ {
1850
+ g , _ = PathUnescape (tc .path )
1851
+ }
1852
+ b .StopTimer ()
1853
+ if g != tc .unescaped {
1854
+ b .Errorf ("PathUnescape(%q) == %q, want %q" , tc .path , g , tc .unescaped )
1855
+ }
1856
+
1857
+ })
1858
+ }
1859
+ }
0 commit comments