diff --git a/src/main/java/spark/ResponseTransformerExtend.java b/src/main/java/spark/ResponseTransformerExtend.java new file mode 100644 index 0000000000..4887f65380 --- /dev/null +++ b/src/main/java/spark/ResponseTransformerExtend.java @@ -0,0 +1,18 @@ +package spark; + + +@FunctionalInterface +public interface ResponseTransformerExtend { + + /** + * Method called for rendering the output. + * + * @param model object used to render output. + * @param req request object to render output. + * @param res response object to render output. + * @return message that it is sent to client. + * @throws java.lang.Exception when render fails + */ + String render(Object model, Request req, Response res) throws Exception; + +} diff --git a/src/test/java/spark/TransformerExtendTest.java b/src/test/java/spark/TransformerExtendTest.java new file mode 100644 index 0000000000..a4507c163e --- /dev/null +++ b/src/test/java/spark/TransformerExtendTest.java @@ -0,0 +1,56 @@ +package spark; + +import org.junit.Before; +import org.junit.Test; +import spark.examples.transformer.MyMessage; +import spark.routematch.RouteMatch; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +//CS304 Issue link: https://github.com/perwendel/spark/issues/534 +public class TransformerExtendTest { + + HttpServletRequest servletRequest; + HttpServletResponse servletResponse; + Request request; + Response response; + RouteMatch match = new RouteMatch(null, "/hi", "/hi", "text/html"); + TransformerExtend transformer; + + @Before + public void setup() { + servletRequest = mock(HttpServletRequest.class); + request = new Request(match, servletRequest); + + + servletResponse = mock(HttpServletResponse.class); + response = new Response(servletResponse); + + transformer = new TransformerExtend(); + } + + @Test + public void testJsonRequest() throws Exception { + final String contentType = "application/json"; + + when(servletRequest.getContentType()).thenReturn(contentType); + + assertEquals("{\"message\":\"Hello World\"}", transformer.render(new MyMessage("Hello World"), request, response)); + + } + + @Test + public void testNotJsonRequest() throws Exception { + final String contentType = "text/xml"; + + when(servletRequest.getContentType()).thenReturn(contentType); + + assertEquals("Not Json type message. Other types are not implemented.", transformer.render(new MyMessage("Hello World"), request, response)); + + } +} diff --git a/src/test/java/spark/examples/transformer/TransformerExtend.java b/src/test/java/spark/examples/transformer/TransformerExtend.java new file mode 100644 index 0000000000..74ab4f29c9 --- /dev/null +++ b/src/test/java/spark/examples/transformer/TransformerExtend.java @@ -0,0 +1,18 @@ +package spark; + +import com.google.gson.Gson; + +//CS304 Issue link: https://github.com/perwendel/spark/issues/534 +public class TransformerExtend implements ResponseTransformerExtend { + + @Override + public String render(Object model, Request req, Response res) throws Exception { + if(req.contentType().equals("application/json")) { + Gson gson = new Gson(); + res.type("application/json"); + System.out.println(res.type()); + return gson.toJson(model); + } + return "Not Json type message. Other types are not implemented."; + } +}