On rest correct single object get to produce jsonp by default. This may have to be correctted in the future
--- a/server/src/main/java/org/iri_research/renkan/rest/RenkanResource.java Tue May 14 12:54:42 2013 +0200
+++ b/server/src/main/java/org/iri_research/renkan/rest/RenkanResource.java Wed Apr 24 16:24:04 2013 +0200
@@ -58,11 +58,13 @@
this.getRepository().delete(objectId);
}
+
+ //TODO: this produce application/javascript by default. I would rather have application/json. The prefered behaviour would be to produde js only od the callbacl query param is used
@GET
@Path("{id : [a-zA-Z\\-0-9]+}")
- @Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8", "application/javascript",
+ @Produces({ "application/javascript",
"application/x-javascript", "text/ecmascript",
- "application/ecmascript", "text/jscript"})
+ "application/ecmascript", "text/jscript", MediaType.APPLICATION_JSON + ";charset=utf-8"})
public JSONWithPadding getObject(@PathParam("id") ID objectId, @QueryParam("callback") @DefaultValue("callback") String callback) {
this.logger.debug("GetObject: " + objectId);
--- a/server/src/test/java/org/iri_research/renkan/test/rest/SpaceRestTest.java Tue May 14 12:54:42 2013 +0200
+++ b/server/src/test/java/org/iri_research/renkan/test/rest/SpaceRestTest.java Wed Apr 24 16:24:04 2013 +0200
@@ -313,7 +313,7 @@
Space space = this.spacesList.get(this.firstSpaceUUID);
WebResource webResource = this.resource();
- String respString = webResource.path("spaces").path(this.firstSpaceUUID).get(String.class);
+ String respString = webResource.path("spaces").path(this.firstSpaceUUID).accept(MediaType.APPLICATION_JSON).get(String.class);
logger.debug("RESPONSE : " + respString);
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(respString);
@@ -328,7 +328,7 @@
Space space = this.spacesList.get(this.firstSpaceUUID);
WebResource webResource = this.resource();
- String respString = webResource.path("spaces").path(this.firstSpaceUUID).get(String.class);
+ String respString = webResource.path("spaces").path(this.firstSpaceUUID).accept(MediaType.APPLICATION_JSON).get(String.class);
logger.debug("RESPONSE : " + respString);
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(respString);
@@ -343,7 +343,7 @@
Space space = this.spacesList.get(this.firstSpaceUUID);
WebResource webResource = this.resource();
- String respString = webResource.path("spaces").path(this.firstSpaceUUID).get(String.class);
+ String respString = webResource.path("spaces").path(this.firstSpaceUUID).accept(MediaType.APPLICATION_JSON).get(String.class);
logger.debug("RESPONSE : " + respString);
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(respString);
@@ -358,7 +358,7 @@
Space space = this.spacesList.get(this.firstSpaceUUID);
WebResource webResource = this.resource();
- String respString = webResource.path("spaces").path(this.firstSpaceUUID).get(String.class);
+ String respString = webResource.path("spaces").path(this.firstSpaceUUID).accept(MediaType.APPLICATION_JSON).get(String.class);
logger.debug("RESPONSE : " + respString);
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(respString);
@@ -375,7 +375,7 @@
Space space = this.spacesList.get(this.firstSpaceUUID);
WebResource webResource = this.resource();
- String respString = webResource.path("spaces").path(this.firstSpaceUUID).get(String.class);
+ String respString = webResource.path("spaces").path(this.firstSpaceUUID).accept(MediaType.APPLICATION_JSON).get(String.class);
logger.debug("RESPONSE : " + respString);
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(respString);
@@ -390,7 +390,7 @@
Space space = this.spacesList.get(this.firstSpaceUUID);
WebResource webResource = this.resource();
- String respString = webResource.path("spaces").path(this.firstSpaceUUID).get(String.class);
+ String respString = webResource.path("spaces").path(this.firstSpaceUUID).accept(MediaType.APPLICATION_JSON).get(String.class);
logger.debug("RESPONSE : " + respString);
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(respString);
@@ -406,7 +406,7 @@
Space space = this.spacesList.get(this.firstSpaceUUID);
WebResource webResource = this.resource();
- String respString = webResource.path("spaces").path(this.firstSpaceUUID).get(String.class);
+ String respString = webResource.path("spaces").path(this.firstSpaceUUID).accept(MediaType.APPLICATION_JSON).get(String.class);
logger.debug("RESPONSE : " + respString);
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(respString);
@@ -751,4 +751,37 @@
Assert.assertEquals("title must be the same", space.getTitle(), jsonNode.get("title").asText());
}
+
+ @Test
+ public void testSingleGetJsonpAcceptAll() throws JsonProcessingException, IOException {
+ WebResource webResource = this.resource();
+ ClientResponse resp = webResource.path("spaces").path(this.firstSpaceUUID).queryParam("callback", "callback_func").accept("*/*").get(ClientResponse.class);
+ Assert.assertEquals("return type must be application/javascript","application/javascript", resp.getType().toString());
+
+ String respStr = resp.getEntity(String.class);
+
+ Assert.assertTrue("resp must start with callback call", respStr.startsWith("callback_func("));
+ Assert.assertTrue("resp must start with callback call", respStr.endsWith(")"));
+
+ String jsonStr = respStr.substring("callback_func(".length(), respStr.length()-1);
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode jsonNode = mapper.readTree(jsonStr);
+
+ Space space = this.spacesList.get(this.firstSpaceUUID);
+
+ String id = jsonNode.get("id").asText();
+ Assert.assertEquals("id must be equals", space.getId(), id);
+
+ Assert.assertTrue("the space must have a created", jsonNode.hasNonNull("created"));
+
+ String dateStr = jsonNode.get("created").asText();
+
+ DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
+ DateTime date = fmt.parseDateTime(dateStr);
+
+ Assert.assertEquals("Created date must be the same", space.getCreated(), date.toDate());
+
+ Assert.assertEquals("title must be the same", space.getTitle(), jsonNode.get("title").asText());
+
+ }
}