Permalink
Browse files

Extended JASPIC CDI forward test with check for servlet path

  • Loading branch information...
1 parent d25a239 commit b8569195070f355d04f8f869f162f13de0dafa53 @arjantijms arjantijms committed Jul 14, 2016
@@ -1,14 +1,23 @@
package org.javaee7.jaspic.dispatching.bean;
import javax.enterprise.context.RequestScoped;
+import javax.inject.Inject;
import javax.inject.Named;
+import javax.servlet.http.HttpServletRequest;
@Named
@RequestScoped
public class MyBean {
+
+ @Inject
+ private HttpServletRequest request;
public String getText() {
- return "Called from CDI";
+ return "Called from CDI\n";
+ }
+
+ public String getServletPath() {
+ return request.getServletPath();
}
}
@@ -1,6 +1,9 @@
package org.javaee7.jaspic.dispatching.servlet;
+import static java.util.logging.Level.SEVERE;
+
import java.io.IOException;
+import java.util.logging.Logger;
import javax.inject.Inject;
import javax.servlet.ServletException;
@@ -21,12 +24,20 @@
private static final long serialVersionUID = 1L;
+ private final static Logger logger = Logger.getLogger(ForwardedServlet.class.getName());
+
@Inject
private MyBean myBean;
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("response from forwardedServlet - " + myBean.getText());
+ response.getWriter().write("servletPath via Servlet - " + request.getServletPath() + "\n");
+ try {
+ response.getWriter().write("servletPath via CDI - " + myBean.getServletPath());
+ } catch (Exception e) {
+ logger.log(SEVERE, "", e);
+ }
}
}
@@ -29,6 +29,12 @@
);
}
+ /**
+ * Tests that the forwarded resource can utilize a CDI bean
+ *
+ * @throws IOException
+ * @throws SAXException
+ */
@Test
public void testCDIForwardViaPublicResource() throws IOException, SAXException {
@@ -39,6 +45,12 @@ public void testCDIForwardViaPublicResource() throws IOException, SAXException {
);
}
+ /**
+ * Tests that the forwarded resource can utilize a CDI bean
+ *
+ * @throws IOException
+ * @throws SAXException
+ */
@Test
public void testCDIForwardViaProtectedResource() throws IOException, SAXException {
@@ -48,5 +60,97 @@ public void testCDIForwardViaProtectedResource() throws IOException, SAXExceptio
response.contains("response from forwardedServlet - Called from CDI")
);
}
+
+ /**
+ * Tests that the forwarded resource has the correct servlet path
+ *
+ * @throws IOException
+ * @throws SAXException
+ */
+ @Test
+ public void testCDIForwardWithRequestPublic() throws IOException, SAXException {
+
+ String response = getFromServerPath("public/servlet");
+
+ assertTrue(
+ "Servletpath reported by servlet request after forward from SAM not as expected.",
+ response.contains("servletPath via Servlet - /forwardedServlet")
+ );
+ }
+
+ /**
+ * Tests that the forwarded resource has the correct servlet path
+ *
+ * @throws IOException
+ * @throws SAXException
+ */
+ @Test
+ public void testCDIForwardWithRequestProtected() throws IOException, SAXException {
+
+ String response = getFromServerPath("protected/servlet");
+
+ assertTrue(
+ "Servletpath reported by servlet request after forward from SAM not as expected.",
+ response.contains("servletPath via Servlet - /forwardedServlet")
+ );
+ }
+
+ /**
+ * Tests that the forwarded resource can utilize an injected HttpServletRequest and that
+ * the value is correct.
+ *
+ * @throws IOException
+ * @throws SAXException
+ */
+ @Test
+ public void testCDIForwardWithRequestInjectPublic() throws IOException, SAXException {
+
+ String response = getFromServerPath("public/servlet");
+
+ assertTrue(
+ "Servletpath reported by servlet request after forward from SAM not as expected.",
+ response.contains("servletPath via Servlet - /forwardedServlet")
+ );
+
+ assertTrue(
+ "Response did not contain output from forwarded Servlet using CDI injected request. " +
+ "Request appears not to be usable.",
+ response.contains("servletPath via CDI")
+ );
+
+ assertTrue(
+ "Servletpath reported by injected request after forward from SAM not as expected.",
+ response.contains("servletPath via CDI - /forwardedServlet")
+ );
+ }
+
+ /**
+ * Tests that the forwarded resource can utilize an injected HttpServletRequest and that
+ * the value is correct.
+ *
+ * @throws IOException
+ * @throws SAXException
+ */
+ @Test
+ public void testCDIForwardWithRequestInjectProtected() throws IOException, SAXException {
+
+ String response = getFromServerPath("protected/servlet");
+
+ assertTrue(
+ "Servletpath reported by servlet request after forward from SAM not as expected.",
+ response.contains("servletPath via Servlet - /forwardedServlet")
+ );
+
+ assertTrue(
+ "Response did not contain output from forwarded Servlet using CDI injected request. " +
+ "Request appears not to be usable.",
+ response.contains("servletPath via CDI")
+ );
+
+ assertTrue(
+ "Servletpath reported by injected request after forward from SAM not as expected.",
+ response.contains("servletPath via CDI - /forwardedServlet")
+ );
+ }
}

0 comments on commit b856919

Please sign in to comment.