diff --git a/src/main/java/spark/DataStore.java b/src/main/java/spark/DataStore.java new file mode 100644 index 0000000000..a68e91a689 --- /dev/null +++ b/src/main/java/spark/DataStore.java @@ -0,0 +1,77 @@ +package spark; + +import java.util.HashMap; + +/** + * Represents the datastore + * you can use it to store some data and find whether it is existence. + */ +public final class DataStore { + static HashMap Store = new HashMap(); + + private DataStore(){ + + } + + /**. + * find whether key is existence + * CS304 Issue link: https://github.com/perwendel/spark/issues/966 + * + * @param key just like key in hashmap + * @return return the result, true or false + */ + public static boolean existence(String key) { + return Store.containsKey(key); + } + + /**. + * find the corresponding value + * CS304 Issue link: https://github.com/perwendel/spark/issues/966 + * + * @param key just like "key" in hashmap + * @return return the corresponding value result + */ + public static String findvalue(String key) { + return Store.get(key); + } + + /**. + * clear the store + * CS304 Issue link: https://github.com/perwendel/spark/issues/966 + */ + public static void clearstore() { + Store.clear(); + } + + /**. + * put a pair into store + * CS304 Issue link: https://github.com/perwendel/spark/issues/966 + * + * @param key just like "key" in hashmap + * @param value just like "value" in hashmap + */ + public static void putstore(String key,String value) { + Store.put(key,value); + } + + /**. + * clear the store + * CS304 Issue link: https://github.com/perwendel/spark/issues/966 + * + * @return return the size of the store + */ + public static int getstoresize() { + return Store.size(); + } + + /**. + * find whether the store is empty + * CS304 Issue link: https://github.com/perwendel/spark/issues/966 + * + * @return return the result, true or false + */ + public static boolean storeisempty() { + return Store.isEmpty(); + } + +} diff --git a/src/main/java/spark/http/matching/Body.java b/src/main/java/spark/http/matching/Body.java index 462a2f7aaf..174504e3ff 100644 --- a/src/main/java/spark/http/matching/Body.java +++ b/src/main/java/spark/http/matching/Body.java @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package spark.http.matching; import java.io.IOException; @@ -22,15 +23,20 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import spark.utils.GzipUtils; import spark.serialization.SerializerChain; +import spark.utils.GzipUtils; + + -/** +/**. * Represents the 'body' */ final class Body { private Object content; + //CS304 Issue link: https://github.com/perwendel/spark/issues/1022 + private boolean useEmpty = false; + public static Body create() { return new Body(); @@ -56,13 +62,27 @@ public void set(Object content) { this.content = content; } + + /**. + * CS304 Issue link: https://github.com/perwendel/spark/issues/1022 + * modify line 80 + * get default response content type + * if useEmpty is set, content type can be null + * otherwise null content type will be set to default + * @param httpResponse response to the request + * @param serializerChain Serialize the body to output stream + * @param httpRequest received request + * @throws IOException throw exception if there is Input/Output error. + */ public void serializeTo(HttpServletResponse httpResponse, SerializerChain serializerChain, HttpServletRequest httpRequest) throws IOException { if (!httpResponse.isCommitted()) { - if (httpResponse.getContentType() == null) { - httpResponse.setContentType("text/html; charset=utf-8"); + if (httpResponse.getContentType() == null && !useEmpty) { + //CS304 Issue link: https://github.com/perwendel/spark/issues/911 + String type = Configuration.getDefaultcontentype(); + httpResponse.setContentType(type); } // Check if GZIP is wanted/accepted and in that case handle that @@ -76,5 +96,14 @@ public void serializeTo(HttpServletResponse httpResponse, } } + /** + * CS304 Issue link: https://github.com/perwendel/spark/issues/1022 + * setter for boolean useEmpty + * @param useEmpty {@code boolean} the value that will change to useEmpty + */ + public void setUseEmpty(boolean useEmpty) { + this.useEmpty = useEmpty; + } + } diff --git a/src/main/java/spark/http/matching/Configuration.java b/src/main/java/spark/http/matching/Configuration.java new file mode 100644 index 0000000000..e9552e4397 --- /dev/null +++ b/src/main/java/spark/http/matching/Configuration.java @@ -0,0 +1,28 @@ +package spark.http.matching; + +public final class Configuration { + private static String defaultcontentype = "application/json"; + + private Configuration() { + } + + /**. + * change default response content type + * CS304 Issue link: https://github.com/perwendel/spark/issues/911 + * + * @param type the default response content type + */ + public static void setDefaultcontentype(String type) { + defaultcontentype = type; + } + + /**. + * get default response content type + * CS304 Issue link: https://github.com/perwendel/spark/issues/911 + * + * @return the default response content type + */ + public static String getDefaultcontentype() { + return defaultcontentype; + } +} diff --git a/src/test/java/spark/BeforeResponseTest.java b/src/test/java/spark/BeforeResponseTest.java new file mode 100644 index 0000000000..0229abe75e --- /dev/null +++ b/src/test/java/spark/BeforeResponseTest.java @@ -0,0 +1,44 @@ +package spark; + +import org.junit.Before; +import org.junit.Test; +import spark.http.matching.Configuration; +import org.junit.Assert; + +import static spark.Spark.*; + +public class BeforeResponseTest { + + @Before + public void setup() { + Configuration.setDefaultcontentype("text/html"); + } + + /**. + * change default response content type in configutaion + * CS304 (manually written) Issue link: https://github.com/perwendel/spark/issues/911 + */ + @Test + public void Testresponsedefaultcontenttype() throws Exception { + get("/hello", (request, response) -> { + Assert.assertEquals("text/html",response.type()); + return "Hello World!"; + } + ); + } + + /**. + * after change default content type, change one content type by filter. + * CS304 (manually written) Issue link: https://github.com/perwendel/spark/issues/911 + */ + @Test + public void Testresponsedefaultcontenttypeaftermodified() throws Exception { + before("/hello", (request, response) -> response.type("application/json")); + get("/hello", (request, response) -> { + Assert.assertEquals("text/html", Configuration.getDefaultcontentype()); + Assert.assertEquals("application/json",response.type()); + return "Hello World!"; + } + ); + } +} diff --git a/src/test/java/spark/Configurationtest.java b/src/test/java/spark/Configurationtest.java new file mode 100644 index 0000000000..1732d6c85a --- /dev/null +++ b/src/test/java/spark/Configurationtest.java @@ -0,0 +1,49 @@ +package spark; + +import org.junit.Assert; +import org.junit.Test; +import spark.http.matching.Configuration; +import spark.util.SparkTestUtil; + + +public class Configurationtest { + /**. + * test get method. + * CS304 (manually written) Issue link: https://github.com/perwendel/spark/issues/911 + */ + @Test + public void gettypetruetest() throws Exception { + Configuration.setDefaultcontentype("application/json"); + Assert.assertEquals("application/json",Configuration.getDefaultcontentype()); + } + + /**. + * test get method. + * CS304 (manually written) Issue link: https://github.com/perwendel/spark/issues/911 + */ + @Test + public void gettypefalsetest() throws Exception { + Configuration.setDefaultcontentype("application/json"); + Assert.assertNotEquals("text/html",Configuration.getDefaultcontentype()); + } + + /**. + * test set method. + * CS304 (manually written) Issue link: https://github.com/perwendel/spark/issues/911 + */ + @Test + public void settypetruetest() throws Exception { + Configuration.setDefaultcontentype("text/html"); + Assert.assertEquals("text/html",Configuration.getDefaultcontentype()); + } + + /**. + * test set method. + * CS304 (manually written) Issue link: https://github.com/perwendel/spark/issues/911 + */ + @Test + public void settypefalsetest() throws Exception { + Configuration.setDefaultcontentype("text/html"); + Assert.assertNotEquals("application/json",Configuration.getDefaultcontentype()); + } +} diff --git a/src/test/java/spark/DataStoreTest.java b/src/test/java/spark/DataStoreTest.java new file mode 100644 index 0000000000..2d884667c3 --- /dev/null +++ b/src/test/java/spark/DataStoreTest.java @@ -0,0 +1,35 @@ +package spark; + +import org.junit.Assert; +import org.junit.Test; +import spark.http.matching.Configuration; + +public class DataStoreTest { + /**. + * test put some info into the store + * CS304 (manually written) Issue link: https://github.com/perwendel/spark/issues/966 + */ + @Test + public void putinstore() throws Exception { + String book = "daughter of sea"; + String author ="Andersen"; + DataStore.putstore(book,author); + Assert.assertTrue(DataStore.existence(book)); + Assert.assertEquals("Andersen",DataStore.findvalue(book)); + } + + /**. + * test clear to the store + * CS304 (manually written) Issue link: https://github.com/perwendel/spark/issues/966 + */ + @Test + public void clearstoreandaddvalue() throws Exception { + DataStore.clearstore(); + Assert.assertEquals(0,DataStore.getstoresize()); + String book = "daughter of sea"; + String author ="Andersen"; + DataStore.putstore(book,author); + Assert.assertEquals("Andersen",DataStore.findvalue(book)); + } + +}