MyMockFileServlet myServlet = new MyMockFileServlet();
myServlet.setRootDir(dir);
MockServletConfig config = new MockServletConfig();
MockServletContext context = new MockServletContext() {
public String getMimeType(String s) {
return "text/html";
}
};
config.setServletContext(context);
myServlet.init(config);
File indexFile = new File(dir, "index.html");
indexFile.deleteOnExit();
assertFalse("cannot test service index if index.html already exists", indexFile.exists());
boolean created = indexFile.createNewFile();
assertTrue(created);
// redirect when no trailing path, even if no index defined.
request.setPathInfo("");
myServlet.service(request, response1);
response1.ensureRedirect("/");
assertEquals(0, myServlet.printDirCalls);
// do not display index if none configured
myServlet.init();
MyMockServletResponse response2 = new MyMockServletResponse();
request.setPathInfo("/");
myServlet.service(request, response2);
String actual = response2.getWritten();
assertTrue(actual.startsWith("<html><body><h1>"));
assertTrue(myServlet.printDirCalls > 0);
String actualMimeType = response2.getContentType();
assertEquals("text/html", actualMimeType);
// use index if one exists when asking for trailing path and fileServlet.indexFiles configured
myServlet.init();
MyMockServletResponse response3 = new MyMockServletResponse();
context.setInitParameter("fileServlet.welcomeFiles", "index.html");
config.setServletContext(context);
myServlet.init(config);
request.setPathInfo("/");
myServlet.service(request, response3);