Skip to content

Commit cfe0479

Browse files
committed
UNIREST-JAVA 84: Post should sort the fields as part of the request
1 parent 268b45b commit cfe0479

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,11 @@
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>

src/main/java/com/mashape/unirest/http/utils/MapUtil.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ a copy of this software and associated documentation files (the
2929
import java.util.List;
3030
import java.util.Map;
3131
import java.util.Map.Entry;
32+
import java.util.TreeMap;
3233

3334
import org.apache.http.NameValuePair;
3435
import 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) {

src/test/java/com/mashape/unirest/test/http/UnirestTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ a copy of this software and associated documentation files (the
3333

3434
import java.io.File;
3535
import java.io.IOException;
36+
import java.io.InputStream;
3637
import java.net.InetAddress;
3738
import java.net.URISyntaxException;
3839
import java.net.UnknownHostException;
@@ -45,6 +46,8 @@ a copy of this software and associated documentation files (the
4546
import java.util.concurrent.TimeUnit;
4647
import java.util.concurrent.atomic.AtomicInteger;
4748

49+
import com.mashape.unirest.request.HttpRequest;
50+
import org.apache.commons.io.IOUtils;
4851
import org.apache.http.impl.client.HttpClientBuilder;
4952
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
5053
import 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
}

0 commit comments

Comments
 (0)