@Test
  public void testRedirectWithSamlIdp() throws Exception {
    String idplist =
        Base64.encodeBytes("http://idp1.com".getBytes())
            + " "
            + Base64.encodeBytes("http://idp2.com".getBytes());
    final Cookie[] c = new Cookie[] {new Cookie("_saml_idp", URLEncoder.encode(idplist, "UTF-8"))};
    c[0].setSecure(true);
    c[0].setPath("/");

    final StringWriter sw = new StringWriter();
    context.checking(
        new Expectations() {
          {
            atLeast(1).of(req).getParameter(DiscoveryServlet.REFERER_PARAMETER);
            will(returnValue("http://localhost"));
            one(req).getCookies();
            will(returnValue(c));
            one(res).getWriter();
            will(returnValue(new PrintWriter(sw)));
            one(res).setContentType("text/html");
          }
        });
    servlet.doGet(req, res);

    assertTrue(
        sw.toString()
                .indexOf(
                    "0;url=http://localhost?_saml_idp="
                        + Base64.encodeBytes(URLEncoder.encode(idplist, "UTF-8").getBytes()))
            > -1);
    context.assertIsSatisfied();
  }
  @Test
  public void failWhenNoReferer() throws Exception {
    context.checking(
        new Expectations() {
          {
            atLeast(1).of(req).getParameter(DiscoveryServlet.REFERER_PARAMETER);
            will(returnValue(null));
            one(res)
                .sendError(
                    with(equal(HttpServletResponse.SC_PRECONDITION_FAILED)),
                    with(any(String.class)));
          }
        });

    servlet.doGet(req, res);
    context.assertIsSatisfied();
  }
  @Test
  public void returnEmptyWhenNoCookie() throws Exception {
    final StringWriter sw = new StringWriter();
    context.checking(
        new Expectations() {
          {
            atLeast(1).of(req).getParameter(DiscoveryServlet.REFERER_PARAMETER);
            will(returnValue("http://localhost"));
            one(req).getCookies();
            will(returnValue(new Cookie[0]));
            one(res).getWriter();
            will(returnValue(new PrintWriter(sw)));
            one(res).setContentType("text/html");
          }
        });
    servlet.doGet(req, res);

    assertTrue(sw.toString().indexOf("0;url=http://localhost?_saml_idp=") > -1);
    context.assertIsSatisfied();
  }