@@ -800,6 +800,16 @@ var unescapeTests = []EscapeTest{
800
800
"" ,
801
801
EscapeError ("%zz" ),
802
802
},
803
+ {
804
+ "a+b" ,
805
+ "a b" ,
806
+ nil ,
807
+ },
808
+ {
809
+ "a%20b" ,
810
+ "a b" ,
811
+ nil ,
812
+ },
803
813
}
804
814
805
815
func TestUnescape (t * testing.T ) {
@@ -808,10 +818,33 @@ func TestUnescape(t *testing.T) {
808
818
if actual != tt .out || (err != nil ) != (tt .err != nil ) {
809
819
t .Errorf ("QueryUnescape(%q) = %q, %s; want %q, %s" , tt .in , actual , err , tt .out , tt .err )
810
820
}
821
+
822
+ in := tt .in
823
+ out := tt .out
824
+ if strings .Contains (tt .in , "+" ) {
825
+ in = strings .Replace (tt .in , "+" , "%20" , - 1 )
826
+ actual , err := PathUnescape (in )
827
+ if actual != tt .out || (err != nil ) != (tt .err != nil ) {
828
+ t .Errorf ("PathUnescape(%q) = %q, %s; want %q, %s" , in , actual , err , tt .out , tt .err )
829
+ }
830
+ if tt .err == nil {
831
+ s , err := QueryUnescape (strings .Replace (tt .in , "+" , "XXX" , - 1 ))
832
+ if err != nil {
833
+ continue
834
+ }
835
+ in = tt .in
836
+ out = strings .Replace (s , "XXX" , "+" , - 1 )
837
+ }
838
+ }
839
+
840
+ actual , err = PathUnescape (in )
841
+ if actual != out || (err != nil ) != (tt .err != nil ) {
842
+ t .Errorf ("PathUnescape(%q) = %q, %s; want %q, %s" , in , actual , err , out , tt .err )
843
+ }
811
844
}
812
845
}
813
846
814
- var escapeTests = []EscapeTest {
847
+ var queryEscapeTests = []EscapeTest {
815
848
{
816
849
"" ,
817
850
"" ,
@@ -839,8 +872,8 @@ var escapeTests = []EscapeTest{
839
872
},
840
873
}
841
874
842
- func TestEscape (t * testing.T ) {
843
- for _ , tt := range escapeTests {
875
+ func TestQueryEscape (t * testing.T ) {
876
+ for _ , tt := range queryEscapeTests {
844
877
actual := QueryEscape (tt .in )
845
878
if tt .out != actual {
846
879
t .Errorf ("QueryEscape(%q) = %q, want %q" , tt .in , actual , tt .out )
@@ -854,6 +887,54 @@ func TestEscape(t *testing.T) {
854
887
}
855
888
}
856
889
890
+ var pathEscapeTests = []EscapeTest {
891
+ {
892
+ "" ,
893
+ "" ,
894
+ nil ,
895
+ },
896
+ {
897
+ "abc" ,
898
+ "abc" ,
899
+ nil ,
900
+ },
901
+ {
902
+ "abc+def" ,
903
+ "abc+def" ,
904
+ nil ,
905
+ },
906
+ {
907
+ "one two" ,
908
+ "one%20two" ,
909
+ nil ,
910
+ },
911
+ {
912
+ "10%" ,
913
+ "10%25" ,
914
+ nil ,
915
+ },
916
+ {
917
+ " ?&=#+%!<>#\" {}|\\ ^[]`☺\t :/@$'()*,;" ,
918
+ "%20%3F&=%23+%25%21%3C%3E%23%22%7B%7D%7C%5C%5E%5B%5D%60%E2%98%BA%09:%2F@$%27%28%29%2A%2C%3B" ,
919
+ nil ,
920
+ },
921
+ }
922
+
923
+ func TestPathEscape (t * testing.T ) {
924
+ for _ , tt := range pathEscapeTests {
925
+ actual := PathEscape (tt .in )
926
+ if tt .out != actual {
927
+ t .Errorf ("PathEscape(%q) = %q, want %q" , tt .in , actual , tt .out )
928
+ }
929
+
930
+ // for bonus points, verify that escape:unescape is an identity.
931
+ roundtrip , err := PathUnescape (actual )
932
+ if roundtrip != tt .in || err != nil {
933
+ t .Errorf ("PathUnescape(%q) = %q, %s; want %q, %s" , actual , roundtrip , err , tt .in , "[no error]" )
934
+ }
935
+ }
936
+ }
937
+
857
938
//var userinfoTests = []UserinfoTest{
858
939
// {"user", "password", "user:password"},
859
940
// {"foo:bar", "~!@#$%^&*()_+{}|[]\\-=`:;'\"<>?,./",
0 commit comments