Created
August 13, 2019 11:44
-
-
Save hackerzhut/7daf1f1a61c0fa9e950506090da2d952 to your computer and use it in GitHub Desktop.
Jetty Test Server Testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tmna.ct.telematics.one.core.filter; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.io.Writer; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.ProtocolException; | |
import java.net.URL; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import org.eclipse.jetty.server.Request; | |
import org.eclipse.jetty.server.Server; | |
import org.eclipse.jetty.server.handler.AbstractHandler; | |
public class TestServer | |
{ | |
public static void main(String[] args) throws Exception | |
{ | |
Server server = new Server(3333); | |
server.setHandler(new AbstractHandler() | |
{ | |
@Override | |
public void handle(String target, Request req, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException | |
{ | |
response.setContentType("text/plain"); | |
Writer w = response.getWriter(); | |
w.write("Runtime free memory : " + Runtime.getRuntime().freeMemory() + "\n"); | |
((Request) request).setHandled(true); | |
} | |
}); | |
server.start(); | |
// client | |
new Thread() | |
{ | |
public void run() | |
{ | |
try | |
{ | |
test(false); | |
test(true); | |
} | |
catch (IOException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
private void test(boolean sendExpect) throws MalformedURLException, IOException, ProtocolException | |
{ | |
long t = System.currentTimeMillis(); | |
String url = "http://localhost:3333/"; | |
URL u = new URL(url); | |
HttpURLConnection conn = (HttpURLConnection) u.openConnection(); | |
byte[] data = "hello".getBytes(); | |
conn.setRequestMethod("POST"); | |
conn.addRequestProperty("Host", "localhost:3333"); | |
if (sendExpect) | |
conn.addRequestProperty("Expect", "100-continue"); | |
conn.addRequestProperty("Content-Length", "" + data.length); | |
conn.addRequestProperty("Content-Type", "binary/octet-stream"); | |
conn.addRequestProperty("User-Agent", "Apache-HttpClient/4.0.3 (java 1.5)"); | |
conn.addRequestProperty("Connection", "keep-alive"); | |
conn.setDoOutput(true); | |
OutputStream out = conn.getOutputStream(); | |
out.write(data); | |
int c = conn.getResponseCode(); | |
InputStream in = conn.getInputStream(); | |
int x; | |
while((x = in.read()) != -1) | |
{ | |
System.out.print((char)x); | |
} | |
System.out.println("Elapsed : " + (System.currentTimeMillis() - t) + " ms " + (sendExpect ? "with expect" : "without expect") ); | |
}; | |
}.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment