File tree Expand file tree Collapse file tree 3 files changed +26
-2
lines changed
main/java/com/mashape/unirest/http/utils
test/java/com/mashape/unirest/test/http Expand file tree Collapse file tree 3 files changed +26
-2
lines changed Original file line number Diff line number Diff line change 126126 <version >4.12</version >
127127 <scope >test</scope >
128128 </dependency >
129+ <dependency >
130+ <groupId >commons-io</groupId >
131+ <artifactId >commons-io</artifactId >
132+ <version >2.4</version >
133+ <scope >test</scope >
134+ </dependency >
129135 </dependencies >
130136</project >
Original file line number Diff line number Diff line change @@ -29,6 +29,7 @@ a copy of this software and associated documentation files (the
2929import java .util .List ;
3030import java .util .Map ;
3131import java .util .Map .Entry ;
32+ import java .util .TreeMap ;
3233
3334import org .apache .http .NameValuePair ;
3435import org .apache .http .message .BasicNameValuePair ;
@@ -38,7 +39,8 @@ public class MapUtil {
3839 public static List <NameValuePair > getList (Map <String , List <Object >> parameters ) {
3940 List <NameValuePair > result = new ArrayList <NameValuePair >();
4041 if (parameters != null ) {
41- for (Entry <String , List <Object >> entry : parameters .entrySet ()) {
42+ TreeMap <String , List <Object >> sortedParameters = new TreeMap <String , List <Object >>(parameters );
43+ for (Entry <String , List <Object >> entry : sortedParameters .entrySet ()) {
4244 List <Object > entryValue = entry .getValue ();
4345 if (entryValue != null ) {
4446 for (Object cur : entryValue ) {
Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ a copy of this software and associated documentation files (the
3333
3434import java .io .File ;
3535import java .io .IOException ;
36+ import java .io .InputStream ;
3637import java .net .InetAddress ;
3738import java .net .URISyntaxException ;
3839import java .net .UnknownHostException ;
@@ -45,6 +46,8 @@ a copy of this software and associated documentation files (the
4546import java .util .concurrent .TimeUnit ;
4647import java .util .concurrent .atomic .AtomicInteger ;
4748
49+ import com .mashape .unirest .request .HttpRequest ;
50+ import org .apache .commons .io .IOUtils ;
4851import org .apache .http .impl .client .HttpClientBuilder ;
4952import org .apache .http .impl .nio .client .HttpAsyncClientBuilder ;
5053import org .json .JSONArray ;
@@ -685,5 +688,18 @@ public void setTimeoutsAndCustomClient() {
685688 // Ok
686689 }
687690 }
688-
691+
692+ @ Test
693+ public void testPostProvidesSortedParams () throws IOException {
694+ // Verify that fields are encoded into the body in sorted order.
695+ HttpRequest httpRequest = Unirest .post ("test" )
696+ .field ("z" , "Z" )
697+ .field ("y" , "Y" )
698+ .field ("x" , "X" )
699+ .getHttpRequest ();
700+
701+ InputStream content = httpRequest .getBody ().getEntity ().getContent ();
702+ String body = IOUtils .toString (content , "UTF-8" );
703+ assertEquals ("x=X&y=Y&z=Z" , body );
704+ }
689705}
You can’t perform that action at this time.
0 commit comments