You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
313 lines
8.2 KiB
313 lines
8.2 KiB
package de.superx.servlet; |
|
|
|
import static org.junit.Assert.assertEquals; |
|
import static org.junit.Assert.assertFalse; |
|
import static org.junit.Assert.assertNull; |
|
import static org.junit.Assert.assertTrue; |
|
import static org.mockito.Mockito.when; |
|
|
|
import java.io.IOException; |
|
import java.io.PrintWriter; |
|
import java.util.ArrayList; |
|
import java.util.Collection; |
|
import java.util.Locale; |
|
|
|
import jakarta.servlet.ServletOutputStream; |
|
import jakarta.servlet.http.Cookie; |
|
import jakarta.servlet.http.HttpServletResponse; |
|
|
|
import org.junit.Before; |
|
import org.junit.Ignore; |
|
import org.junit.Test; |
|
|
|
public class SuperXServletHelperTest extends BaseServletTest { |
|
|
|
private CustomHttpServletResponse customHttpServletResponse= new CustomHttpServletResponse(); |
|
|
|
private final String HOST_NAME = "host_name"; |
|
|
|
private final String SESSION_ID_COOKIE_PATH = "/superx"; |
|
|
|
private final String SSO_COOKIE_PATH = "/"; |
|
|
|
private final Cookie SESSION_ID_COOKIE = new Cookie("JSESSIONID", "1234567890"); |
|
|
|
private final Cookie SIGNLE_SIGN_ON_COOKIE = new Cookie("JSESSIONIDSSO", "1234567890"); |
|
|
|
@Before |
|
public void prepareMocks() { |
|
when(this.request.getCookies()).thenReturn(new Cookie[] { this.SESSION_ID_COOKIE, this.SIGNLE_SIGN_ON_COOKIE }); |
|
when(this.request.getHeader("host")).thenReturn("." + this.HOST_NAME); |
|
} |
|
|
|
@Test |
|
public void testRemoveCookie() { |
|
when(this.request.getScheme()).thenReturn("https"); |
|
SuperXServletHelper.removeCookie(this.request, this.customHttpServletResponse); |
|
Collection<Cookie> cookieList = this.customHttpServletResponse.getCookies(); |
|
for (Cookie responseCookie : cookieList) { |
|
if (responseCookie.getName().equals("JSESSIONID")) { |
|
assertEquals("path has been set to", this.SESSION_ID_COOKIE_PATH, responseCookie.getPath()); |
|
} |
|
if (responseCookie.getName().equals("JSESSIONIDSSO")) { |
|
assertEquals("path has been set to", this.SSO_COOKIE_PATH, responseCookie.getPath()); |
|
} |
|
assertEquals("domain has been set", this.HOST_NAME, responseCookie.getDomain()); |
|
assertTrue("security has been set to true", responseCookie.getSecure()); |
|
} |
|
} |
|
|
|
@Test |
|
public void testRemoveCookieSetSecurityToFalse() { |
|
when(this.request.getScheme()).thenReturn("http"); |
|
CustomHttpServletResponse customHttpServletResponse = new CustomHttpServletResponse(); |
|
SuperXServletHelper.removeCookie(this.request, customHttpServletResponse); |
|
Collection<Cookie> cookieList = customHttpServletResponse.getCookies(); |
|
for (Cookie responseCookie : cookieList) { |
|
if (responseCookie.getName().equals("JSESSIONID")) { |
|
assertEquals("path has been set to", this.SESSION_ID_COOKIE_PATH, responseCookie.getPath()); |
|
} |
|
if (responseCookie.getName().equals("JSESSIONIDSSO")) { |
|
assertEquals("path has been set to", this.SSO_COOKIE_PATH, responseCookie.getPath()); |
|
} |
|
assertEquals("domain has been set", this.HOST_NAME, responseCookie.getDomain()); |
|
assertFalse("security has been set to false", responseCookie.getSecure()); |
|
} |
|
} |
|
|
|
@Test |
|
public void testRemoveCookieWithOtherCookies() { |
|
Cookie cookie = new Cookie("cookie", "cookie"); |
|
when(this.request.getCookies()).thenReturn(new Cookie[] { cookie }); |
|
CustomHttpServletResponse customHttpServletResponse = new CustomHttpServletResponse(); |
|
SuperXServletHelper.removeCookie(this.request, customHttpServletResponse); |
|
assertNull("path has not been set", cookie.getPath()); |
|
assertNull("domain has not been set", cookie.getDomain()); |
|
assertFalse("security has been set to false", cookie.getSecure()); |
|
} |
|
} |
|
|
|
/** |
|
* Wrapper class to make cookies available for testing |
|
* |
|
* @author hiber |
|
* |
|
*/ |
|
@Ignore // to avoid error message 'No runnable methods' |
|
class CustomHttpServletResponse implements HttpServletResponse { |
|
|
|
private Collection<Cookie> cookies = new ArrayList<Cookie>(); |
|
|
|
public CustomHttpServletResponse() { |
|
// empty constructor |
|
} |
|
|
|
@Override |
|
public void flushBuffer() throws IOException { |
|
// TODO Auto-generated method stub |
|
|
|
} |
|
|
|
@Override |
|
public int getBufferSize() { |
|
// TODO Auto-generated method stub |
|
return 0; |
|
} |
|
|
|
@Override |
|
public String getCharacterEncoding() { |
|
// TODO Auto-generated method stub |
|
return null; |
|
} |
|
|
|
@Override |
|
public String getContentType() { |
|
// TODO Auto-generated method stub |
|
return null; |
|
} |
|
|
|
@Override |
|
public Locale getLocale() { |
|
// TODO Auto-generated method stub |
|
return null; |
|
} |
|
|
|
@Override |
|
public ServletOutputStream getOutputStream() throws IOException { |
|
// TODO Auto-generated method stub |
|
return null; |
|
} |
|
|
|
@Override |
|
public PrintWriter getWriter() throws IOException { |
|
// TODO Auto-generated method stub |
|
return null; |
|
} |
|
|
|
@Override |
|
public boolean isCommitted() { |
|
// TODO Auto-generated method stub |
|
return false; |
|
} |
|
|
|
@Override |
|
public void reset() { |
|
// TODO Auto-generated method stub |
|
|
|
} |
|
|
|
@Override |
|
public void resetBuffer() { |
|
// TODO Auto-generated method stub |
|
|
|
} |
|
|
|
@Override |
|
public void setBufferSize(int arg0) { |
|
// TODO Auto-generated method stub |
|
|
|
} |
|
|
|
@Override |
|
public void setCharacterEncoding(String arg0) { |
|
// TODO Auto-generated method stub |
|
|
|
} |
|
|
|
@Override |
|
public void setContentLength(int arg0) { |
|
// TODO Auto-generated method stub |
|
|
|
} |
|
|
|
@Override |
|
public void setContentType(String arg0) { |
|
// TODO Auto-generated method stub |
|
|
|
} |
|
|
|
@Override |
|
public void setLocale(Locale arg0) { |
|
// TODO Auto-generated method stub |
|
|
|
} |
|
|
|
@Override |
|
public void addCookie(Cookie cookie) { |
|
this.cookies.add(cookie); |
|
|
|
} |
|
|
|
public Collection<Cookie> getCookies() { |
|
return this.cookies; |
|
} |
|
|
|
@Override |
|
public void addDateHeader(String arg0, long arg1) { |
|
// TODO Auto-generated method stub |
|
|
|
} |
|
|
|
@Override |
|
public void addHeader(String arg0, String arg1) { |
|
// TODO Auto-generated method stub |
|
|
|
} |
|
|
|
@Override |
|
public void addIntHeader(String arg0, int arg1) { |
|
// TODO Auto-generated method stub |
|
|
|
} |
|
|
|
@Override |
|
public boolean containsHeader(String arg0) { |
|
// TODO Auto-generated method stub |
|
return false; |
|
} |
|
|
|
@Override |
|
public String encodeRedirectURL(String arg0) { |
|
// TODO Auto-generated method stub |
|
return null; |
|
} |
|
|
|
@Override |
|
public String encodeURL(String arg0) { |
|
// TODO Auto-generated method stub |
|
return null; |
|
} |
|
|
|
@Override |
|
public String getHeader(String arg0) { |
|
// TODO Auto-generated method stub |
|
return null; |
|
} |
|
|
|
@Override |
|
public Collection<String> getHeaderNames() { |
|
// TODO Auto-generated method stub |
|
return null; |
|
} |
|
|
|
@Override |
|
public Collection<String> getHeaders(String arg0) { |
|
// TODO Auto-generated method stub |
|
return null; |
|
} |
|
|
|
@Override |
|
public int getStatus() { |
|
// TODO Auto-generated method stub |
|
return 0; |
|
} |
|
|
|
@Override |
|
public void sendError(int arg0) throws IOException { |
|
// TODO Auto-generated method stub |
|
|
|
} |
|
|
|
@Override |
|
public void sendError(int arg0, String arg1) throws IOException { |
|
// TODO Auto-generated method stub |
|
|
|
} |
|
|
|
@Override |
|
public void sendRedirect(String arg0) throws IOException { |
|
// TODO Auto-generated method stub |
|
|
|
} |
|
|
|
@Override |
|
public void setDateHeader(String arg0, long arg1) { |
|
// TODO Auto-generated method stub |
|
|
|
} |
|
|
|
@Override |
|
public void setHeader(String arg0, String arg1) { |
|
// TODO Auto-generated method stub |
|
|
|
} |
|
|
|
@Override |
|
public void setIntHeader(String arg0, int arg1) { |
|
// TODO Auto-generated method stub |
|
|
|
} |
|
|
|
@Override |
|
public void setStatus(int arg0) { |
|
// TODO Auto-generated method stub |
|
|
|
} |
|
|
|
@Override |
|
public void setContentLengthLong(long arg0) { |
|
// TODO Auto-generated method stub |
|
|
|
} |
|
|
|
} |