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

fix for issue 911 and 966 #1173

Open
wants to merge 2 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
77 changes: 77 additions & 0 deletions src/main/java/spark/DataStore.java
Original file line number Diff line number Diff line change
@@ -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<String, String> Store = new HashMap<String, String>();

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();
}

}
37 changes: 33 additions & 4 deletions src/main/java/spark/http/matching/Body.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -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;
}


}
28 changes: 28 additions & 0 deletions src/main/java/spark/http/matching/Configuration.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
44 changes: 44 additions & 0 deletions src/test/java/spark/BeforeResponseTest.java
Original file line number Diff line number Diff line change
@@ -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!";
}
);
}
}
49 changes: 49 additions & 0 deletions src/test/java/spark/Configurationtest.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 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());
}
}
35 changes: 35 additions & 0 deletions src/test/java/spark/DataStoreTest.java
Original file line number Diff line number Diff line change
@@ -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));
}

}