Changed folder structure of test classes and renamed some packages

This commit is contained in:
Ziver Koc 2016-03-04 17:48:59 +01:00
parent ccd50ec104
commit 884c5d64c3
76 changed files with 5305 additions and 5375 deletions

View file

@ -0,0 +1,102 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Ziver Koc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package zutil.net.http;
import java.io.IOException;
import java.util.Map;
public class HTTPGuessTheNumber implements HttpPage{
public static void main(String[] args) throws IOException{
//HttpServer server = new HttpServer("localhost", 443, FileFinder.find("keySSL"), "rootroot");//SSL
HttpServer server = new HttpServer(8080);
server.setDefaultPage(new HTTPGuessTheNumber());
server.run();
}
public void respond(HttpPrintStream out,
HttpHeader client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request) throws IOException {
out.enableBuffering(true);
out.println("<html>");
out.println("<H2>Welcome To The Number Guess Game!</H2>");
if(session.containsKey("random_nummber") && request.containsKey("guess") && !request.get("guess").isEmpty()){
int guess = Integer.parseInt(request.get("guess"));
int nummber = (Integer)session.get("random_nummber");
try {
if(guess == nummber){
session.remove("random_nummber");
out.println("You Guessed Right! Congrats!");
out.println("</html>");
return;
}
else if(guess > nummber){
out.println("<b>To High</b><br>");
if(Integer.parseInt(cookie.get("high")) > guess){
out.setCookie("high", ""+guess);
cookie.put("high", ""+guess);
}
}
else{
out.println("<b>To Low</b><br>");
if(Integer.parseInt(cookie.get("low")) < guess){
out.setCookie("low", ""+guess);
cookie.put("low", ""+guess);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
else{
session.put("random_nummber", (int)(Math.random()*99+1));
try {
out.setCookie("low", "0");
out.setCookie("high", "100");
cookie.put("low", "0");
cookie.put("high", "100");
} catch (Exception e) {
e.printStackTrace();
}
}
out.println("<form method='post'>");
out.println(cookie.get("low")+" < X < "+cookie.get("high")+"<br>");
out.println("Guess a number between 0 and 100:<br>");
out.println("<input type='text' name='guess'>");
out.println("<input type='hidden' name='test' value='test'>");
out.println("<input type='submit' value='Guess'>");
out.println("</form>");
out.println("<script>document.all.guess.focus();</script>");
out.println("<b>DEBUG: nummber="+session.get("random_nummber")+"</b><br>");
out.println("</html>");
}
}

View file

@ -0,0 +1,57 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Ziver Koc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package zutil.net.http;
import java.io.IOException;
import java.util.Map;
public class HTTPUploaderTest implements HttpPage{
public static void main(String[] args) throws IOException{
HttpServer server = new HttpServer(80);
server.setDefaultPage(new HTTPUploaderTest());
server.run();
}
public void respond(HttpPrintStream out,
HttpHeader client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request) throws IOException {
if(!session.containsKey("file1")){
out.println("</html>" +
" <form enctype='multipart/form-data' method='post'>" +
" <p>Please specify a file, or a set of files:<br>" +
" <input type='file' name='datafile' size='40'>" +
" </p>" +
" <input type='submit' value='Send'>" +
" </form>" +
"</html>");
}
}
}

View file

@ -0,0 +1,74 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Ziver Koc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package zutil.net.http;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
public class HttpURLTest {
@Test
public void fullURLTest() {
HttpURL url = new HttpURL();
url.setProtocol("http");
assertEquals( "http://127.0.0.1/", url.getURL() );
url.setHost("koc.se");
assertEquals( "http://koc.se/", url.getURL() );
url.setPort( 80 );
assertEquals( "http://koc.se:80/", url.getURL() );
url.setPath("test/index.html");
assertEquals( "http://koc.se:80/test/index.html", url.getURL() );
url.setParameter("key", "value");
assertEquals( "http://koc.se:80/test/index.html?key=value", url.getURL() );
url.setAnchor( "anch" );
assertEquals( "http://koc.se:80/test/index.html?key=value#anch", url.getURL() );
}
@Test
public void urlParameterTest() {
HttpURL url = new HttpURL();
url.setParameter("key1", "value1");
assertEquals( "key1=value1", url.getParameterString() );
url.setParameter("key1", "value1");
assertEquals( "key1=value1", url.getParameterString() );
url.setParameter("key2", "value2");
assertThat(url.getParameterString(), allOf(containsString("key2=value2"), containsString("key1=value1")));
}
}