@Test public void testGetRequest() throws Exception { BufferedWriter out; String filename = "test-login.csv"; out = new BufferedWriter(new FileWriter(new File(DATA_DIR, filename))); out.write("login1;password1"); out.write(CRLF); out.write("login2;password2"); out.write(CRLF); out.close(); HttpSimpleTableServer obj = new HttpSimpleTableServerEmul(-1, true, DATA_DIR); String result = sendHttpGet(obj, "" + "/sts"); assertTrue(0 < result.length() && result .startsWith("<html><head><title>URL for the dataset</title><head>")); result = sendHttpGet(obj, "" + "/sts/"); assertTrue(0 < result.length() && result .startsWith("<html><head><title>URL for the dataset</title><head>")); result = sendHttpGet(obj, "" + "/sts/STATUS"); assertEquals("<html><title>KO</title>" + CRLF + "<body>" + "Error : Database was empty !</body>" + CRLF + "</html>", result); result = sendHttpGet(obj, "/sts/INITFILE", this.createParm("FILENAME", filename)); assertEquals("<html><title>OK</title>" + CRLF + "<body>2</body>" + CRLF + "</html>", result); result = sendHttpGet(obj, "/sts/INITFILE", this.createParm("FILENAME", "unknown.txt")); assertEquals("<html><title>KO</title>" + CRLF + "<body>Error : file not found !</body>" + CRLF + "</html>", result); result = sendHttpGet(obj, "/sts/INITFILE", new HashMap<String, String>()); assertEquals("<html><title>KO</title>" + CRLF + "<body>Error : FILENAME parameter was missing !</body>" + CRLF + "</html>", result); File dataset = new File(DATA_DIR, filename); dataset.delete(); Map<String, String> map1 = this.createParm("FILENAME", filename); map1.put("READ_MODE", "LAST"); result = sendHttpGet(obj, "/sts/READ", map1); assertEquals("<html><title>OK</title>" + CRLF + "<body>login2;password2</body>" + CRLF + "</html>", result); Map<String, String> map2 = this.createParm("FILENAME", filename); map2.put("READ_MODE", "FIRST"); map2.put("KEEP", "FALSE"); result = sendHttpGet(obj, "/sts/READ", map2); assertEquals("<html><title>OK</title>" + CRLF + "<body>login1;password1</body>" + CRLF + "</html>", result); Map<String, String> map3 = this.createParm("FILENAME", filename); map3.put("READ_MODE", "SECOND"); result = sendHttpGet(obj, "/sts/READ", map3); assertEquals( "<html><title>KO</title>" + CRLF + "<body>Error : READ_MODE value has to be FIRST, LAST or RANDOM !</body>" + CRLF + "</html>", result); Map<String, String> map4 = this.createParm("A", filename); map4.put("READ_MODE", "LAST"); result = sendHttpGet(obj, "/sts/READ", map4); assertEquals("<html><title>KO</title>" + CRLF + "<body>Error : FILENAME parameter was missing !</body>" + CRLF + "</html>", result); result = sendHttpGet(obj, "/sts/READ", this.createParm("FILENAME", "unexpected.txt")); assertEquals("<html><title>KO</title>" + CRLF + "<body>Error : unexpected.txt not loaded yet !</body>" + CRLF + "</html>", result); Map<String, String> map5 = this.createParm("FILENAME", filename); map5.put("KEEP", "NO"); result = sendHttpGet(obj, "/sts/READ", map5); assertEquals("<html><title>KO</title>" + CRLF + "<body>Error : KEEP value has to be TRUE or FALSE !</body>" + CRLF + "</html>", result); result = sendHttpGet(obj, "/sts/LENGTH", this.createParm("FILENAME", filename)); assertEquals("<html><title>OK</title>" + CRLF + "<body>1</body>" + CRLF + "</html>", result); result = sendHttpPost(obj, "/sts/LENGTH", this.createParm("FILENAME", filename)); assertEquals("<html><title>OK</title>" + CRLF + "<body>1</body>" + CRLF + "</html>", result); result = sendHttpGet(obj, "/sts/LENGTH", this.createParm("FILENAME", "unknown.txt")); assertEquals("<html><title>KO</title>" + CRLF + "<body>Error : unknown.txt not loaded yet !</body>" + CRLF + "</html>", result); result = sendHttpGet(obj, "/sts/LENGTH", this.createParm("A", "unknown.txt")); assertEquals("<html><title>KO</title>" + CRLF + "<body>Error : FILENAME parameter was missing !</body>" + CRLF + "</html>", result); Map<String, String> urlParameters = this.createParm("FILENAME", "unknown.txt"); urlParameters.put("ADD_MODE", "LAST"); urlParameters.put("FILENAME", "test-login.csv"); urlParameters.put("LINE", "login3;password3"); result = sendHttpPost(obj, "/sts/ADD", urlParameters); assertEquals("<html><title>OK</title>" + CRLF + "<body></body>" + CRLF + "</html>", result); urlParameters.put("UNIQUE", "TRUE"); result = sendHttpPost(obj, "/sts/ADD", urlParameters); assertEquals("<html><title>KO</title>" + CRLF + "<body>Error : ENTRY already exists !</body>" + CRLF + "</html>", result); urlParameters.put("UNIQUE", "FALSE"); result = sendHttpPost(obj, "/sts/ADD", urlParameters); assertEquals("<html><title>OK</title>" + CRLF + "<body></body>" + CRLF + "</html>", result); urlParameters.put("UNIQUE", "UNKNOWN"); result = sendHttpPost(obj, "/sts/ADD", urlParameters); assertEquals("<html><title>KO</title>" + CRLF + "<body>Error : UNIQUE value has to be TRUE or FALSE !</body>" + CRLF + "</html>", result); result = sendHttpGet(obj, "" + "/sts/ADD?LINE=login4;password4&FILENAME=" + filename); assertEquals("<html><title>KO</title>" + CRLF + "<body>Error : unknown command !</body>" + CRLF + "</html>", result); Map<String, String> urlParameters2 = this.createParm("FILENAME", "unknown.txt"); urlParameters2.put("ADD_MODE", "LAST"); urlParameters2.put("FILENAME", "test-login.csv"); result = sendHttpPost(obj, "/sts/ADD", urlParameters2); assertEquals("<html><title>KO</title>" + CRLF + "<body>Error : LINE parameter was missing !</body>" + CRLF + "</html>", result); Map<String, String> urlParameters3 = this.createParm("FILENAME", "unknown.txt"); urlParameters3.put("FILENAME", "test-login.csv"); urlParameters3.put("LINE", "login3;password3"); result = sendHttpPost(obj, "/sts/ADD", urlParameters3); assertEquals("<html><title>OK</title>" + CRLF + "<body></body>" + CRLF + "</html>", result); Map<String, String> urlParameters4 = this.createParm("FILENAME", "unknown.txt"); urlParameters4.put("ADD_MODE", "RANDOM"); urlParameters4.put("FILENAME", "test-login.csv"); urlParameters4.put("LINE", "login3;password3"); result = sendHttpPost(obj, "/sts/ADD", urlParameters4); assertEquals( "<html><title>KO</title>" + CRLF + "<body>Error : ADD_MODE value has to be FIRST or LAST !</body>" + CRLF + "</html>", result); Map<String, String> urlParameters5 = this.createParm("FILENAME", filename); urlParameters4.put("READ_MODE", "RANDOM"); result = sendHttpGet(obj, "/sts/READ", urlParameters5); assertTrue(result.startsWith("<html><title>OK</title>")); result = sendHttpGet(obj, "/sts/SAVE", this.createParm("FILENAME", filename)); assertEquals("<html><title>OK</title>" + CRLF + "<body>4</body>" + CRLF + "</html>", result); result = sendHttpGet(obj, "/sts/SAVE", this.createParm("FILENAME", "aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiiijjjjjjjjjjkkkkkkkkkkllllllllllmmmmmmmmmm.txt" + filename)); assertEquals("<html><title>KO</title>" + CRLF + "<body>Error : Maximum size reached (128) !</body>" + CRLF + "</html>", result); result = sendHttpGet(obj, "/sts/SAVE", this.createParm("FILENAME", "logins:passwords.csv")); assertEquals("<html><title>KO</title>" + CRLF + "<body>Error : Illegal character found !</body>" + CRLF + "</html>", result); result = sendHttpGet(obj, "/sts/SAVE", this.createParm("FILENAME", ".")); assertEquals("<html><title>KO</title>" + CRLF + "<body>Error : Illegal character found !</body>" + CRLF + "</html>", result); result = sendHttpGet(obj, "/sts/SAVE", this.createParm("FILENAME", "..")); assertEquals("<html><title>KO</title>" + CRLF + "<body>Error : Illegal character found !</body>" + CRLF + "</html>", result); dataset = new File(DATA_DIR, filename); dataset.delete(); result = sendHttpGet(obj, "/sts/RESET", this.createParm("FILENAME", filename)); assertEquals("<html><title>OK</title>" + CRLF + "<body></body>" + CRLF + "</html>", result); result = sendHttpGet(obj, "/sts/RESET", this.createParm("A", filename)); assertEquals("<html><title>KO</title>" + CRLF + "<body>Error : FILENAME parameter was missing !</body>" + CRLF + "</html>", result); result = sendHttpGet(obj, "/sts/READ", this.createParm("FILENAME", filename)); assertEquals("<html><title>KO</title>" + CRLF + "<body>Error : No more line !</body>" + CRLF + "</html>", result); result = sendHttpGet(obj, "/sts/STATUS"); assertEquals("<html><title>OK</title>" + CRLF + "<body>" + CRLF + filename + " = 0<br />" + CRLF + "</body></html>", result); }
private String length(String filename) { if (null == filename) { return "<html><title>KO</title>" + lineSeparator + "<body>Error : FILENAME parameter was missing !</body>" + lineSeparator + "</html>"; } if (!database.containsKey(filename)) { return "<html><title>KO</title>" + lineSeparator + "<body>Error : " + filename + " not loaded yet !</body>" + lineSeparator + "</html>"; } return "<html><title>OK</title>" + lineSeparator + "<body>" + database.get(filename).size() + "</body>" + lineSeparator + "</html>"; }
HttpSimpleTableServer extends NanoHTTPD implements Stoppable, KeyWaiter { private String length(String filename) { if (null == filename) { return "<html><title>KO</title>" + lineSeparator + "<body>Error : FILENAME parameter was missing !</body>" + lineSeparator + "</html>"; } if (!database.containsKey(filename)) { return "<html><title>KO</title>" + lineSeparator + "<body>Error : " + filename + " not loaded yet !</body>" + lineSeparator + "</html>"; } return "<html><title>OK</title>" + lineSeparator + "<body>" + database.get(filename).size() + "</body>" + lineSeparator + "</html>"; } }
HttpSimpleTableServer extends NanoHTTPD implements Stoppable, KeyWaiter { private String length(String filename) { if (null == filename) { return "<html><title>KO</title>" + lineSeparator + "<body>Error : FILENAME parameter was missing !</body>" + lineSeparator + "</html>"; } if (!database.containsKey(filename)) { return "<html><title>KO</title>" + lineSeparator + "<body>Error : " + filename + " not loaded yet !</body>" + lineSeparator + "</html>"; } return "<html><title>OK</title>" + lineSeparator + "<body>" + database.get(filename).size() + "</body>" + lineSeparator + "</html>"; } HttpSimpleTableServer(int port, boolean timestamp, String dataDir); }
HttpSimpleTableServer extends NanoHTTPD implements Stoppable, KeyWaiter { private String length(String filename) { if (null == filename) { return "<html><title>KO</title>" + lineSeparator + "<body>Error : FILENAME parameter was missing !</body>" + lineSeparator + "</html>"; } if (!database.containsKey(filename)) { return "<html><title>KO</title>" + lineSeparator + "<body>Error : " + filename + " not loaded yet !</body>" + lineSeparator + "</html>"; } return "<html><title>OK</title>" + lineSeparator + "<body>" + database.get(filename).size() + "</body>" + lineSeparator + "</html>"; } HttpSimpleTableServer(int port, boolean timestamp, String dataDir); @Override Response serve(IHTTPSession session); @Override void stopServer(); static void main(String args[]); void waitForKey(); }
HttpSimpleTableServer extends NanoHTTPD implements Stoppable, KeyWaiter { private String length(String filename) { if (null == filename) { return "<html><title>KO</title>" + lineSeparator + "<body>Error : FILENAME parameter was missing !</body>" + lineSeparator + "</html>"; } if (!database.containsKey(filename)) { return "<html><title>KO</title>" + lineSeparator + "<body>Error : " + filename + " not loaded yet !</body>" + lineSeparator + "</html>"; } return "<html><title>OK</title>" + lineSeparator + "<body>" + database.get(filename).size() + "</body>" + lineSeparator + "</html>"; } HttpSimpleTableServer(int port, boolean timestamp, String dataDir); @Override Response serve(IHTTPSession session); @Override void stopServer(); static void main(String args[]); void waitForKey(); static final String STS_VERSION; static final String ROOT; static final String ROOT2; static final String URI_INITFILE; static final String URI_READ; static final String URI_ADD; static final String URI_SAVE; static final String URI_LENGTH; static final String URI_STATUS; static final String URI_RESET; static final String URI_STOP; static final String PARM_FILENAME; static final String PARM_LINE; static final String PARM_READ_MODE; static final String PARM_ADD_MODE; static final String PARM_UNIQUE; static final String PARM_KEEP; static final String VAL_FIRST; static final String VAL_LAST; static final String VAL_RANDOM; static final String VAL_TRUE; static final String VAL_FALSE; static final String INDEX; static String lineSeparator; }