Skip to content

Commit

Permalink
Handle primitive json responses (#364)
Browse files Browse the repository at this point in the history
* handle primitive json responses

* incorporate review comments
  • Loading branch information
tsiq-karold committed Jun 25, 2019
1 parent ecbde06 commit 19be4c8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
Expand Up @@ -1159,6 +1159,17 @@ class HttpGroovyTest implements HttpConfiguration {
}
}

@Test
void "handles integer json responses"() {
def ret = http.get('/integer') {
body.should == 123
return body
}

ret.should == 123
ret.getClass().should == Integer
}

@Override
String fullUrl(String url) {
if (UrlUtils.isFull(url)) {
Expand Down
7 changes: 2 additions & 5 deletions webtau-http/src/main/java/com/twosigma/webtau/http/Http.java
Expand Up @@ -558,11 +558,8 @@ private DataNode createBodyDataNode(HttpResponse response) {
}

if (response.isJson()) {
Object mapOrList = JsonUtils.deserialize(response.getTextContent());

return mapOrList instanceof List ?
DataNodeBuilder.fromList(id, (List<Object>) mapOrList) :
DataNodeBuilder.fromMap(id, (Map<String, Object>) mapOrList);
Object object = JsonUtils.deserialize(response.getTextContent());
return DataNodeBuilder.fromValue(id, object);
}

return new StructuredDataNode(id, new TraceableValue(response.getBinaryContent()));
Expand Down
Expand Up @@ -57,7 +57,7 @@ private static List<DataNode> buildListOfNodes(DataNodeId id, List<Object> value
}

@SuppressWarnings("unchecked")
private static DataNode fromValue(DataNodeId id, Object value) {
public static DataNode fromValue(DataNodeId id, Object value) {
if (value instanceof Map) {
return new StructuredDataNode(id, buildMapOfNodes(id, (Map<String, Object>)value));
} else if (value instanceof List) {
Expand Down
Expand Up @@ -270,6 +270,17 @@ public void queryParams() {
});
}

@Test
public void handlesIntegerJsonResponses() {
Integer ret = http.get("/integer", (header, body) -> {
body.should(equal(123));
return body;
});

actual(ret).should(equal(123));
actual(ret.getClass()).should(equal(Integer.class));
}

@Override
public String fullUrl(String url) {
if (UrlUtils.isFull(url)) {
Expand Down
Expand Up @@ -68,6 +68,7 @@ public class HttpTestDataServer {
testServer.registerDelete("/resource", new TestServerTextResponse("abc"));
testServer.registerGet("/params?a=1&b=text", new TestServerJsonResponse("{\"a\": 1, \"b\": \"text\"}"));
testServer.registerPost("/params?a=1&b=text", new TestServerJsonResponse("{\"a\": 1, \"b\": \"text\"}", 201));
testServer.registerGet("/integer", new TestServerJsonResponse("123"));

registerRedirects();
}
Expand Down

0 comments on commit 19be4c8

Please sign in to comment.