Created
October 5, 2010 10:33
-
-
Save MoriTanosuke/611340 to your computer and use it in GitHub Desktop.
This file contains 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
import java.io.IOException; | |
import java.io.PrintWriter; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
public class HelloServlet extends HttpServlet { | |
@Override | |
public void doGet(HttpServletRequest req, HttpServletResponse resp) | |
throws ServletException, IOException { | |
PrintWriter out = resp.getWriter(); | |
out.println("<h1>Hello, World!</h1>"); | |
out.flush(); | |
} | |
} |
This file contains 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
import static org.junit.Assert.*; | |
import java.util.Vector; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.mortbay.jetty.testing.HttpTester; | |
import org.mortbay.jetty.testing.ServletTester; | |
public class HelloServletTest { | |
private ServletTester tester; | |
@Before | |
public void setUp() throws Exception { | |
tester = new ServletTester(); | |
tester.setContextPath("/context"); | |
tester.addServlet(HelloServlet.class, "/hello/*"); | |
tester.start(); | |
} | |
@After | |
public void tearDown() throws Exception { | |
tester.stop(); | |
} | |
@Test | |
public void testDefaultServlet() throws Exception { | |
HttpTester request = withRequest("/context/hello/info", null); | |
HttpTester response = new HttpTester(); | |
response.parse(tester.getResponses(request.generate())); | |
assertNull(response.getMethod()); | |
assertEquals(200, response.getStatus()); | |
assertEquals("<h1>Hello, World!</h1>", response.getContent().trim()); | |
} | |
private HttpTester withRequest(String path, Vector<String[]> parameters) { | |
String requestParameters = ""; | |
for (String[] params : parameters) { | |
requestParameters += ("".equals(requestParameters) ? "?" : "&"); | |
requestParameters += params[0] + "=" + params[1]; | |
} | |
String uri = path; | |
if (requestParameters != "") | |
uri += requestParameters; | |
HttpTester request = new HttpTester(); | |
request.setMethod("GET"); | |
request.setHeader("Host", "127.0.0.1"); | |
request.setURI(uri); | |
request.setVersion("HTTP/1.0"); | |
return request; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment