Skip to content

Instantly share code, notes, and snippets.

@cstamas
Last active November 20, 2015 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cstamas/90fc4b40a3cf9cb83ada to your computer and use it in GitHub Desktop.
Save cstamas/90fc4b40a3cf9cb83ada to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.net.URI;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
public class JettyTest
{
private Server server;
private HttpClient client;
@Before
public void prepare() throws Exception {
server = new Server(8081);
server.setHandler(new AbstractHandler()
{
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
baseRequest.setHandled(true);
response.getWriter().println("<h1>Hello World</h1>");
}
});
server.start();
client = new HttpClient();
client.start();
}
@After
public void cleanup() throws Exception {
if (client != null) {
client.stop();
}
if (server != null) {
server.stop();
}
}
@Test
public void isOk1() throws Exception {
ContentResponse response = client.GET("http://localhost:8081/foo/bar");
assertThat(response.getStatus(), equalTo(HttpServletResponse.SC_OK));
}
@Test
public void isOk2() throws Exception {
ContentResponse response = client.GET("http://localhost:8081/foo/@bar");
assertThat(response.getStatus(), equalTo(HttpServletResponse.SC_OK));
}
@Test
public void whyIsBadURL() throws Exception {
ContentResponse response = client.GET("http://localhost:8081/@foo/bar");
assertThat(response.getStatus(), equalTo(HttpServletResponse.SC_OK));
}
@Test
public void javaVsJetty() throws Exception {
String uriStr = "/@foo/bar";
URI javaUri = new URI(uriStr);
HttpURI jettyUri = new HttpURI(uriStr);
assertThat("[" + uriStr + "] .scheme", javaUri.getScheme(), is(jettyUri.getScheme()));
assertThat("[" + uriStr + "] .host", javaUri.getHost(), is(jettyUri.getHost()));
assertThat("[" + uriStr + "] .port", javaUri.getPort(), is(jettyUri.getPort()));
assertThat("[" + uriStr + "] .path", javaUri.getPath(), is(jettyUri.getPath()));
assertThat("[" + uriStr + "] .query", javaUri.getQuery(), is(jettyUri.getQuery()));
assertThat("[" + uriStr + "] .fragment", javaUri.getFragment(), is(jettyUri.getFragment()));
assertThat("[" + uriStr + "] .toString", javaUri.toString(), is(jettyUri.toString()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment