Skip to content
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
7 changes: 6 additions & 1 deletion src/main/java/spark/http/matching/Body.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ final class Body {

private Object content;

//CS304 Issue link: https://github.com/perwendel/spark/issues/911
private final Configuration configuration = Configuration.getConfiguration();

public static Body create() {
return new Body();
}
Expand Down Expand Up @@ -62,7 +65,9 @@ public void serializeTo(HttpServletResponse httpResponse,

if (!httpResponse.isCommitted()) {
if (httpResponse.getContentType() == null) {
httpResponse.setContentType("text/html; charset=utf-8");
//CS304 Issue link: https://github.com/perwendel/spark/issues/911
String type = configuration.getDefaultContentType();
httpResponse.setContentType(type);
}

// Check if GZIP is wanted/accepted and in that case handle that
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/spark/http/matching/Configuration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package spark.http.matching;

//CS304 Issue link: https://github.com/perwendel/spark/issues/911
public class Configuration {
private String defaultContentType;
private static final Configuration configuration = new Configuration();

private Configuration() {
defaultContentType = "text/html; charset=utf-8";
}

public static Configuration getConfiguration(){
return configuration;
}

public void setDefaultContentType(String type) {
defaultContentType = type;
}

public String getDefaultContentType() {
return defaultContentType;
}
}
49 changes: 49 additions & 0 deletions src/test/java/spark/issue911Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package spark;

import org.junit.Assert;
import org.junit.Test;
import org.junit.Before;
import spark.http.matching.Configuration;

import static spark.Spark.*;

public class issue911Test {
Configuration configuration;

//CS304 Issue link: https://github.com/perwendel/spark/issues/911
@Before
public void init(){
configuration = Configuration.getConfiguration();
}

//CS304 Issue link: https://github.com/perwendel/spark/issues/911
@Test
public void TestResponseDefaultContentType() throws Exception {
get("/hello", (request, response) -> {
Assert.assertEquals("text/html; charset=utf-8", response.type());
return "Hello World!";
});
}

//CS304 Issue link: https://github.com/perwendel/spark/issues/911
@Test
public void TestResponseTypeModifiedByBefore() throws Exception {
before("/hello", (request, response) -> response.type("application/json"));
get("/hello", (request, response) -> {
Assert.assertEquals("text/html; charset=utf-8", configuration.getDefaultContentType());
Assert.assertEquals("application/json", response.type());
return "Hello World!";
});
}

//CS304 Issue link: https://github.com/perwendel/spark/issues/911
@Test
public void TestSetDefaultContentType() throws Exception {
configuration.setDefaultContentType("text/html");
get("/hello", (request, response) -> {
Assert.assertEquals("text/html",configuration.getDefaultContentType());
Assert.assertEquals("text/html", response.type());
return "Hello World!";
});
}
}