Skip to content

Commit

Permalink
Added ability to represent iterables as repeated parameters (for thos…
Browse files Browse the repository at this point in the history
…e parameters that end in "[]").

Upgrade hamcrest.
Relates to #50.
  • Loading branch information
mmazi committed May 25, 2015
1 parent d38106f commit 9a15ea2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<artifactId>hamcrest-junit</artifactId>
<version>2.0.0.0</version>
<scope>test</scope>
</dependency>

Expand Down
24 changes: 17 additions & 7 deletions src/main/java/si/mazi/rescu/Params.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -86,12 +87,21 @@ public Params add(String param, Object value) {

private String toQueryString(boolean encode) {
StringBuilder b = new StringBuilder();
for (String param : data.keySet()) {
if (isParamSet(param)) {
if (b.length() > 0) {
b.append('&');
for (String paramName : data.keySet()) {
if (isParamSet(paramName)) {
Object originalValue = getParamValue(paramName);
boolean createArrayParameters = originalValue instanceof Iterable && paramName.endsWith("[]");
@SuppressWarnings("unchecked")
Iterable<Object> paramValues = createArrayParameters
? (Iterable<Object>)originalValue
: Collections.singleton(originalValue);
for (Object paramValue : paramValues) {
if (b.length() > 0) {
b.append('&');
}
String paramValueAsString = toString(paramValue);
b.append(paramName).append('=').append(encode(paramValueAsString, encode));
}
b.append(param).append('=').append(encode(getParamValueAsString(param), encode));
}
}
return b.toString();
Expand Down Expand Up @@ -147,12 +157,12 @@ static String toString(Object paramValue) {
if (paramValue instanceof BigDecimal) {
return ((BigDecimal) paramValue).toPlainString();
} else if (paramValue instanceof Iterable) {
return collectionToString((Iterable) paramValue);
return iterableToString((Iterable) paramValue);
}
return paramValue.toString();
}

static String collectionToString(Iterable iterable) {
static String iterableToString(Iterable iterable) {
final StringBuilder sb = new StringBuilder();
for (Object o : iterable) {
if (sb.length() > 0) {
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/si/mazi/rescu/ExampleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
* @author Matija Mazi
Expand Down Expand Up @@ -127,4 +128,13 @@ public interface ExampleService {
@Consumes(MediaType.APPLICATION_JSON)
DummyTicker testGetMethodWithBody(DummyAccountInfo ticker) throws IOException;

@POST
@Path("formPostCollection")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
Object testFromPostCollection(@FormParam("data") List<String> data);

@POST
@Path("formPostCollection")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
Object testFromPostCollectionAsArray(@FormParam("data[]") List<String> data);
}
28 changes: 28 additions & 0 deletions src/test/java/si/mazi/rescu/RestInvocationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@
import javax.ws.rs.HeaderParam;
import javax.ws.rs.core.MediaType;
import java.lang.annotation.Annotation;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.testng.Assert.assertEquals;

/**
Expand Down Expand Up @@ -88,4 +92,28 @@ public void testCreateWithValueGenerator() {
assertEquals(invocation.getParamValue(FormParam.class, "nonce"), nonce);
}

@Test
public void testFormPostCollectionDefault() throws Exception {
TestRestInvocationHandler testHandler = new TestRestInvocationHandler(ExampleService.class, new ClientConfig(), null, 200);
ExampleService proxy = RestProxyFactory.createProxy(ExampleService.class, testHandler);

proxy.testFromPostCollection(Arrays.asList("first", "second"));

final String requestBody = URLDecoder.decode(testHandler.getInvocation().getRequestBody(), "UTF-8");
assertThat(requestBody, containsString("data=first,second"));
}

@Test
public void testFormPostCollectionArray() throws Exception {
ClientConfig config = new ClientConfig();

TestRestInvocationHandler testHandler = new TestRestInvocationHandler(ExampleService.class, config, null, 200);
ExampleService proxy = RestProxyFactory.createProxy(ExampleService.class, testHandler);

proxy.testFromPostCollectionAsArray(Arrays.asList("first", "second"));

final String requestBody = testHandler.getInvocation().getRequestBody();
assertThat(requestBody, containsString("data[]=first"));
assertThat(requestBody, containsString("data[]=second"));
}
}

0 comments on commit 9a15ea2

Please sign in to comment.