Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to fix issue 534 #1174

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/spark/ResponseTransformerExtend.java
Original file line number Diff line number Diff line change
@@ -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;

}
56 changes: 56 additions & 0 deletions src/test/java/spark/TransformerExtendTest.java
Original file line number Diff line number Diff line change
@@ -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));

}
}
18 changes: 18 additions & 0 deletions src/test/java/spark/examples/transformer/TransformerExtend.java
Original file line number Diff line number Diff line change
@@ -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.";
}
}