Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

#310 - Add test checking the 'withJsonBody' behavior

Extend the test application with a simple action returning the json body received in the request body.
Add a test using this action and checking the withJsonBody behavior.
  • Loading branch information...
commit 89c393be56dc7efdd8aadce0c4a0a62e0ce07c74 1 parent fb7ecca
@cescoffier authored
View
6 framework/test/integrationtest-java/app/controllers/Application.java
@@ -1,5 +1,6 @@
package controllers;
+import org.codehaus.jackson.JsonNode;
import play.*;
import play.mvc.*;
@@ -16,5 +17,10 @@ public static Result index(String name) {
public static Result key() {
return ok("Key=" + Play.application().configuration().getString("key"));
}
+
+ public static Result getIdenticalJson() {
+ JsonNode node = request().body().asJson();
+ return ok(node);
+ }
}
View
3  framework/test/integrationtest-java/conf/routes
@@ -6,5 +6,8 @@ GET / controllers.Application.index(name = "Guest"
GET /key controllers.Application.key
GET /:name controllers.Application.index(name)
+POST /json controllers.Application.getIdenticalJson()
+
+
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)
View
25 framework/test/integrationtest-java/test/SimpleTest.java
@@ -8,6 +8,7 @@
import org.codehaus.jackson.JsonNode;
import org.junit.*;
+import play.libs.Json;
import play.mvc.*;
import play.test.*;
import play.data.DynamicForm;
@@ -104,4 +105,28 @@ public void run() {
}
});
}
+
+ /**
+ * Checks that we can build fake request with a json body.
+ */
+ @Test
+ public void withJsonBody() {
+ running(fakeApplication(), new Runnable() {
+ @Override
+ public void run() {
+ Map map = new HashMap();
+ map.put("key1", "val1");
+ map.put("key2", 2);
+ map.put("key3", true);
+ JsonNode node = Json.toJson(map);
+ Result result = routeAndCall(fakeRequest("POST", "/json").withJsonBody(node));
+ assertThat(status(result)).isEqualTo(OK);
+ assertThat(contentType(result)).isEqualTo("application/json");
+ JsonNode node2 = Json.parse(contentAsString(result));
+ assertThat(node2.get("key1").asText()).isEqualTo("val1");
+ assertThat(node2.get("key2").asInt()).isEqualTo(2);
+ assertThat(node2.get("key3").asBoolean()).isTrue();
+ }
+ });
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.