### Question:
LoginServlet extends HttpServlet { public String dropPassword(final String content) { String tmp = content; if (content != null && content.contains("password")) { tmp = tmp.replaceAll("[&]?password=([^&|#]*)?", ""); } return tmp; }  String dropPassword(final String content);  }### Answer:
@Test public void testPasswordIsDroppedWhenParameterIsLast() throws Exception { final LoginServlet servlet = new LoginServlet(); final String cleanQueryString = servlet.dropPassword("?username=walter.bates&password=bpm"); assertThat(cleanQueryString, is("?username=walter.bates")); }
@Test public void testPasswordIsDroppedWhenParameterIsBeforeHash() throws Exception { final LoginServlet servlet = new LoginServlet(); final String cleanQueryString = servlet.dropPassword("?username=walter.bates&password=bpm#hash"); assertThat(cleanQueryString, is("?username=walter.bates#hash")); }
@Test public void testUrlIsDroppedWhenParameterIsFirstAndBeforeHash() throws Exception { final LoginServlet servlet = new LoginServlet(); final String cleanQueryString = servlet.dropPassword("?username=walter.bates&password=bpm#hash"); assertThat(cleanQueryString, is("?username=walter.bates#hash")); }
@Test public void testUrlStayTheSameIfNoPasswordArePresent() throws Exception { final LoginServlet servlet = new LoginServlet(); final String cleanQueryString = servlet.dropPassword("?param=value#dhash"); assertThat(cleanQueryString, is("?param=value#dhash")); }
@Test public void testPasswordIsDroppedEvenIfQueryMarkUpIsntThere() throws Exception { final LoginServlet servlet = new LoginServlet(); final String cleanQueryString = servlet.dropPassword("password=bpm#dhash1&dhash2"); assertThat(cleanQueryString, is("#dhash1&dhash2")); }
@Test public void testUrlStayEmptyIfParameterIsEmpty() throws Exception { final LoginServlet servlet = new LoginServlet(); final String cleanQueryString = servlet.dropPassword(""); assertThat(cleanQueryString, is("")); }
@Test public void testDropPasswordOnRealUrl() throws Exception { final LoginServlet servlet = new LoginServlet(); final String cleanUrl = servlet .dropPassword( "?username=walter.bates&password=bpm&redirectUrl=http%3A%2F%2Flocalhost%3A8080%2Fbonita%2Fportal%2Fhomepage%3Fui%3Dform%26locale%3Den%23form%3DPool-\n" + "-1.0%24entry%26process%3D8506394779365952706%26mode%3Dapp"); assertThat(cleanUrl, is("?username=walter.bates&redirectUrl=http%3A%2F%2Flocalhost%3A8080%2Fbonita%2Fportal%2Fhomepage%3Fui%3Dform%26locale%3Den%23form%3DPool-\n" + "-1.0%24entry%26process%3D8506394779365952706%26mode%3Dapp")); }