private String employee = "http://localhost:8080/employee/";
private String identity = "http://localhost:8080/idp/";
public void testAuthForIDPServletAndSPFilter() throws Exception {
String id = IDGenerator.create("ID_");
SAML2Request saml2Request = new SAML2Request();
AuthnRequestType art = saml2Request.createAuthnRequestType(id, employee, identity, employee);
ServletContext servletContext = new MockServletContext();
// First we go to the employee application
MockContextClassLoader mclSPEmp = setupTCL(profile + "/sp/employee");
Thread.currentThread().setContextClassLoader(mclSPEmp);
SPFilter spEmpl = new SPFilter();
MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
filterConfig.addInitParameter(GeneralConstants.IGNORE_SIGNATURES, "true");
spEmpl.init(filterConfig);
MockHttpSession filterSession = new MockHttpSession();
MockHttpServletRequest filterRequest = new MockHttpServletRequest(filterSession, "POST");
MockHttpServletResponse filterResponse = new MockHttpServletResponse();
ByteArrayOutputStream filterbaos = new ByteArrayOutputStream();
filterResponse.setOutputStream(filterbaos);
spEmpl.doFilter(filterRequest, filterResponse, new MockFilterChain());
String spResponse = new String(filterbaos.toByteArray());
Document spHTMLResponse = DocumentUtil.getDocument(spResponse);
NodeList nodes = spHTMLResponse.getElementsByTagName("INPUT");
Element inputElement = (Element) nodes.item(0);
String idpResponse = inputElement.getAttributeNode("VALUE").getValue();
@SuppressWarnings("unused")
String relayState = null;
if (nodes.getLength() > 1)
relayState = ((Element) nodes.item(1)).getAttributeNode("VALUE").getValue();
// Lets call the IDPServlet
MockHttpSession session = new MockHttpSession();
servletContext = new MockServletContext();
session.setServletContext(servletContext);
IdentityServer server = this.getIdentityServer(session);
servletContext.setAttribute("IDENTITY_SERVER", server);
MockServletConfig servletConfig = new MockServletConfig(servletContext);
MockContextClassLoader mclIDP = setupTCL(profile + "/idp");
Thread.currentThread().setContextClassLoader(mclIDP);
MockHttpServletRequest request = new MockHttpServletRequest(session, "POST");
request.addHeader("Referer", "http://localhost:8080/employee/");
request.addParameter(GeneralConstants.USERNAME_FIELD, "anil");
request.addParameter(GeneralConstants.PASS_FIELD, "anil");
MockHttpServletResponse response = new MockHttpServletResponse();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
response.setOutputStream(baos);
IDPLoginServlet login = new IDPLoginServlet();
login.init(servletConfig);
String samlAuth = DocumentUtil.getDocumentAsString(saml2Request.convert(art));
String samlMessage = Base64.encodeBytes(samlAuth.getBytes());
session.setAttribute("SAMLRequest", samlMessage);
login.testPost(request, response);