@@ -196,11 +196,20 @@ public final class HttpUrlTest {
196
196
assertEquals (HttpUrl .parse ("http://user@host/path" ), HttpUrl .parse ("http://user@host/path" ));
197
197
}
198
198
199
+ /** Given multiple '@' characters, the last one is the delimiter. */
199
200
@ Test public void authorityWithMultipleAtSigns () throws Exception {
200
- assertEquals (HttpUrl .parse ("http://foo%40bar@baz/path" ),
201
- HttpUrl .parse ("http://foo@bar@baz/path" ));
202
- assertEquals (HttpUrl .parse ("http://foo:pass1%40bar%3Apass2@baz/path" ),
203
- HttpUrl .parse ("http://foo:pass1@bar:pass2@baz/path" ));
201
+ HttpUrl httpUrl = HttpUrl .parse ("http://foo@bar@baz/path" );
202
+ assertEquals ("foo@bar" , httpUrl .username ());
203
+ assertEquals ("" , httpUrl .password ());
204
+ assertEquals (HttpUrl .parse ("http://foo%40bar@baz/path" ), httpUrl );
205
+ }
206
+
207
+ /** Given multiple ':' characters, the first one is the delimiter. */
208
+ @ Test public void authorityWithMultipleColons () throws Exception {
209
+ HttpUrl httpUrl = HttpUrl .parse ("http://foo:pass1@bar:pass2@baz/path" );
210
+ assertEquals ("foo" , httpUrl .username ());
211
+ assertEquals ("pass1@bar:pass2" , httpUrl .password ());
212
+ assertEquals (HttpUrl .parse ("http://foo:pass1%40bar%3Apass2@baz/path" ), httpUrl );
204
213
}
205
214
206
215
@ Test public void usernameAndPassword () throws Exception {
@@ -930,19 +939,103 @@ public final class HttpUrlTest {
930
939
assertEquals ("http://host/?d=abc!@[]%5E%60%7B%7D%7C%5C" , uri .toString ());
931
940
}
932
941
933
- @ Test public void toUriSpecialPathCharacters () throws Exception {
942
+ @ Test public void toUriWithUsernameNoPassword () throws Exception {
943
+ HttpUrl httpUrl = new HttpUrl .Builder ()
944
+ .scheme ("http" )
945
+ .username ("user" )
946
+ .host ("host" )
947
+ .build ();
948
+ assertEquals ("http://user@host/" , httpUrl .toString ());
949
+ assertEquals ("http://user@host/" , httpUrl .uri ().toString ());
950
+ }
951
+
952
+ @ Test public void toUriUsernameSpecialCharacters () throws Exception {
934
953
HttpUrl url = new HttpUrl .Builder ()
935
954
.scheme ("http" )
936
- .host ("example.com" )
937
- .addPathSegment ("data=[out:json];node[\" name\" ~\" Karlsruhe\" ]" +
938
- "[\" place\" ~\" city|village|town\" ];out body;" )
955
+ .host ("host" )
956
+ .username ("=[]:;\" ~|?#@^/$%*" )
957
+ .build ();
958
+ assertEquals ("http://%3D%5B%5D%3A%3B%22~%7C%3F%23%40%5E%2F$%25*@host/" , url .toString ());
959
+ assertEquals ("http://%3D%5B%5D%3A%3B%22~%7C%3F%23%40%5E%2F$%25*@host/" , url .uri ().toString ());
960
+ }
961
+
962
+ @ Test public void toUriPasswordSpecialCharacters () throws Exception {
963
+ HttpUrl url = new HttpUrl .Builder ()
964
+ .scheme ("http" )
965
+ .host ("host" )
966
+ .username ("user" )
967
+ .password ("=[]:;\" ~|?#@^/$%*" )
939
968
.build ();
940
- URI uri = url .uri ();
941
- assertEquals ("http://example.com/data=%5Bout:json%5D;node%5B%22name%22~%22Karlsruhe%22%5D" +
942
- "%5B%22place%22~%22city%7Cvillage%7Ctown%22%5D;out%20body;" ,
943
- uri .toString ());
969
+ assertEquals ("http://user:%3D%5B%5D%3A%3B%22~%7C%3F%23%40%5E%2F$%25*@host/" , url .toString ());
970
+ assertEquals ("http://user:%3D%5B%5D%3A%3B%22~%7C%3F%23%40%5E%2F$%25*@host/" ,
971
+ url .uri ().toString ());
972
+ }
973
+
974
+ @ Test public void toUriPathSpecialCharacters () throws Exception {
975
+ HttpUrl url = new HttpUrl .Builder ()
976
+ .scheme ("http" )
977
+ .host ("host" )
978
+ .addPathSegment ("=[]:;\" ~|?#@^/$%*" )
979
+ .build ();
980
+ assertEquals ("http://host/=[]:;%22~%7C%3F%23@%5E%2F$%25*" , url .toString ());
981
+ assertEquals ("http://host/=%5B%5D:;%22~%7C%3F%23@%5E%2F$%25*" , url .uri ().toString ());
944
982
}
945
983
984
+ @ Test public void toUriQueryParameterNameSpecialCharacters () throws Exception {
985
+ HttpUrl url = new HttpUrl .Builder ()
986
+ .scheme ("http" )
987
+ .host ("host" )
988
+ .addQueryParameter ("=[]:;\" ~|?#@^/$%*" , "a" )
989
+ .build ();
990
+ assertEquals ("http://host/?%3D[]:;%22~|?%23@^/$%25*=a" , url .toString ());
991
+ assertEquals ("http://host/?%3D[]:;%22~%7C?%23@%5E/$%25*=a" , url .uri ().toString ());
992
+ }
993
+
994
+ @ Test public void toUriQueryParameterValueSpecialCharacters () throws Exception {
995
+ HttpUrl url = new HttpUrl .Builder ()
996
+ .scheme ("http" )
997
+ .host ("host" )
998
+ .addQueryParameter ("a" , "=[]:;\" ~|?#@^/$%*" )
999
+ .build ();
1000
+ assertEquals ("http://host/?a=%3D[]:;%22~|?%23@^/$%25*" , url .toString ());
1001
+ assertEquals ("http://host/?a=%3D[]:;%22~%7C?%23@%5E/$%25*" , url .uri ().toString ());
1002
+ }
1003
+
1004
+ @ Test public void toUriQueryValueSpecialCharacters () throws Exception {
1005
+ HttpUrl url = new HttpUrl .Builder ()
1006
+ .scheme ("http" )
1007
+ .host ("host" )
1008
+ .query ("=[]:;\" ~|?#@^/$%*" )
1009
+ .build ();
1010
+ assertEquals ("http://host/?=[]:;%22~|?%23@^/$%25*" , url .toString ());
1011
+ assertEquals ("http://host/?=[]:;%22~%7C?%23@%5E/$%25*" , url .uri ().toString ());
1012
+ }
1013
+
1014
+ @ Test public void toUriFragmentSpecialCharacters () throws Exception {
1015
+ HttpUrl url = new HttpUrl .Builder ()
1016
+ .scheme ("http" )
1017
+ .host ("host" )
1018
+ .fragment ("=[]:;\" ~|?#@^/$%*" )
1019
+ .build ();
1020
+ assertEquals ("http://host/#=[]:;\" ~|?#@^/$%25*" , url .toString ());
1021
+ assertEquals ("http://host/#=[]:;%22~%7C?%23@%5E/$%25*" , url .uri ().toString ());
1022
+ }
1023
+
1024
+ @ Test public void toUriWithMalformedPercentEscape () throws Exception {
1025
+ HttpUrl url = new HttpUrl .Builder ()
1026
+ .scheme ("http" )
1027
+ .host ("host" )
1028
+ .encodedPath ("/%xx" )
1029
+ .build ();
1030
+ assertEquals ("http://host/%xx" , url .toString ());
1031
+ try {
1032
+ url .uri ();
1033
+ fail ();
1034
+ } catch (IllegalStateException expected ) {
1035
+ assertEquals ("not valid as a java.net.URI: http://host/%xx" , expected .getMessage ());
1036
+ }
1037
+ }
1038
+
946
1039
@ Test public void fromJavaNetUrl () throws Exception {
947
1040
URL javaNetUrl = new URL ("http://username:password@host/path?query#fragment" );
948
1041
HttpUrl httpUrl = HttpUrl .get (javaNetUrl );
0 commit comments